以Django/Python格式上载特定文件
我在django项目中有一个上传文件表单(我使用的是1.10)。实际上,一切都很好,但由于人们将向服务器上传csv文件,我需要将我的逻辑限制为仅请求具有特定名称和格式的文件 这是我的代码: 视图.py以Django/Python格式上载特定文件,python,html,django,if-statement,file-upload,Python,Html,Django,If Statement,File Upload,我在django项目中有一个上传文件表单(我使用的是1.10)。实际上,一切都很好,但由于人们将向服务器上传csv文件,我需要将我的逻辑限制为仅请求具有特定名称和格式的文件 这是我的代码: 视图.py def list(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('action'))
else:
messages.add_message(request, messages.ERROR,
'Something went wrong with files! Please contact the system administrator')
return render(request, 'upaction-report.html')
# Load documents for the list page
# Render list page with the documents and the form
return render(request, 'upaction-report.html', {'form': form})
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='File format should be under the following format:',
help_text='- .csv format and under the name "Action_Report"',
)
forms.py
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('action'))
else:
messages.add_message(request, messages.ERROR,
'Something went wrong with files! Please contact the system administrator')
return render(request, 'upaction-report.html')
# Load documents for the list page
# Render list page with the documents and the form
return render(request, 'upaction-report.html', {'form': form})
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='File format should be under the following format:',
help_text='- .csv format and under the name "Action_Report"',
)
模板html
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }}</p>
<p> {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload File"/></p>
</form>
{%csrf_令牌%}
{{form.non_field_errors}}
{{form.docfile.label_tag}}
{{form.docfile.help_text}
{{form.docfile}
你猜怎么应用这个限制吗?如果文件不正确,只需弹出一条消息,告诉您再试一次,直到它具有正确的格式和名称。谢谢您的时间,如果您需要更多详细信息,请告诉我。您可以在DocumentForm
clean
方法中验证文件名,如下所示:
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='File format should be under the following format:',
help_text='- .csv format and under the name "Action_Report"',
)
def clean(self):
docfile = self.cleaned_data.get('docfile')
if not docfile:
raise forms.ValidationError('file is required')
if not docfile.name.startswith('filename'):
raise forms.ValidationError('incorrect file name')
if not docfile.name.endswith('fileformat'):
raise forms.ValidationError('incorrect file format')
return super(DocumentForm, self).clean()
嗨,别担心,谢谢你抽出时间。我到底该怎么办?进入我的视图。py?@Deluq不,你应该在DocumentForm类中尝试。我更新了我的答案。哦,好的,知道了。我得到一个错误AttributeError:“非类型”对象没有属性“名称”。我应该在某处设置文件名和文件格式吗?@Deluq您选择了任何文件吗?我想您看到这个错误是因为没有选择任何文件。您需要做的是验证docfile是否为None。请参阅更新的答案。@Deluq是的,没错。只需将文件名和csv作为格式插入即可。