所以,我总是在一个单独的项目中测试新的东西,我用来测试这个项目的例子起了作用。但是,尝试在我的项目中集成相同的内容并不能给我带来我需要的结果。我已经花了将近4个小时在这件事上,到目前为止什么也没有。
首先,下面是代码:
index.html
<form id="data" enctype="multipart/form-data">
<div class="col-md-4">
<label class="file">
<input type="file" id="file1" name="document" multiple>
<span class="file-custom">Documents</span>
<button type="button" onclick="enterdata()">Submit</button>
</div>
</form>
<script>
function enterdata(){
if ($"data")[0].checkValidity(){
alert('validity success');
var token = ''{{ csrf_token }};
alert('csrf generated');
$.ajax({
type:'POST',
url:'/user',
data: {
doc1:$('file1').val()
},
header: {'X-CSRFToken': token},
success: function(){
alert("Added");
$('#data').trigger("reset");
}
})
}else{
$('data')[0].reportValidity()
}
}
</script>
views.py
def testing_data(request):
if request.method == 'POST':
doc11 = request.POST['doc1']
request_file = request.FILES['document'] if 'document' in request.FILES else None
if request_file:
# save attatched file
# create a new instance of FileSystemStorage
fs = FileSystemStorage()
file = fs.save(request_file.name, request_file)
# the fileurl variable now contains the url to the file. This can be used to serve the file when needed.
fileurl = fs.url(file)
landform.objects.create
(
mutdoc=doc11,
)
return HttpResponse('')
models.py
class landform(models.Model):
mutdoc = models.CharField(max_length=255)
urls.py
urlpatterns = [
path('user', testing_data),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
因此,当前的代码正在选择路径,并将其放在模型中的mutdoc字段中,这是正常的,而且我还有很多其他字段,我在上面的代码中忽略了这些字段。但除此之外,当我在另一个项目中独立运行(没有Ajax)时,获取文件并将其保存在媒体目录中的代码工作正常。但是在我的项目中,当我将它结合到上面的视图和Ajax中时,除了Django创建一个媒体文件夹并将所选的文件放入其中之外,其他所有东西都可以正常工作。
爬满了大量的其他,所以线程,博客帖子和视频,但仍然无法使它工作。
原始
html
<form method = 'POST' class="col s12" enctype="multipart/form-data">
{% csrf_token %}
{{new_form.as_p}}
<!--Below is our main file upload input -->
<input type = "file" name = 'document'>
<p><button type = "submit" class = "waves-effect waves-light btn">Publish</button></p>
</form>
views.py
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
def loadpage(request):
if request.method == "POST":
# if the post request has a file under the input name 'document', then save the file.
request_file = request.FILES['document'] if 'document' in request.FILES else None
if request_file:
# save attatched file
# create a new instance of FileSystemStorage
fs = FileSystemStorage()
file = fs.save(request_file.name, request_file)
# the fileurl variable now contains the url to the file. This can be used to serve the file when needed.
fileurl = fs.url(file)
return render(request, "template.html")
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', loadpage)
]
# only in development
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
发布于 2020-12-01 17:54:36
1.替换:
urlpatterns = [
path('user', testing_data),
]
使用
urlpatterns = [
path('user/', views.testing_data, name='testing_data'),
]
2.这是问题所在。试试这个:
取代:
<input type="file" id="file1" name="document" multiple>
通过以下方式:
<input type="file" id="file1" name="document" >
我找不到关于在multiple
中使用<input>
是否会迫使您使用request.FILES.getlist('document')
而不是request.FILES['document']
的文档,但值得一试。
https://stackoverflow.com/questions/65092687
复制相似问题