django - request.FILES 返回姜戈
问题描述
我的代码请求有什么问题。文件返回 <MultiValueDict: {}>
你能帮我解决这个问题吗?
我是 django 的初学者
models.py(这里使用的是reconsider_attachment)
class TaskDocument(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='task_documents')
attachment = models.FileField(upload_to='task_attachments/', blank = True, null = True)
created_at = models.DateTimeField(auto_now_add=True)
document_tag = models.CharField(max_length = 30, blank = True, null = True)
reconsider_attachment = models.FileField(blank = True, null = True)
表格.py
class AppealAddAttachmentForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for myField in self.fields:
self.fields[myField].widget.attrs['class'] = 'form-control'
self.fields['reconsider_attachment'].required = False
class Meta:
model = TaskDocument
fields = ['reconsider_attachment',]
class AppealOTForm(forms.Form):
description = forms.CharField(widget = forms.Textarea(attrs = {'rows':'3'}))
视图.py
def ot_appeal_view(request, pk):
task = get_object_or_404(Task, pk=pk)
if request.method == 'POST':
appeal_ot_form_description = AppealOTForm(request.POST)
appeal_ot_form_file = AppealAddAttachmentForm(request.POST , request.FILES)
if appeal_ot_form_description.is_valid():
description = appeal_ot_form_description.cleaned_data['description']
task.appeal_description = description
task.status = "FOR RE-APPROVAL"
task.save()
if appeal_ot_form_file.is_valid():
file = appeal_ot_form_file.save(commit = False)
file.task = task
print(request.FILES , request.POST)
file.save()
return redirect('ot_task_list')
html 模板 (MODAL)
{% load crispy_forms_tags %}
<form action="" method="POST" id="appealForm" enctype="multipart/form-data">
{% csrf_token %}
<div class="modal fade" id="appealModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Appeal OT</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{appeal_ot_form_description|crispy}}
{{appeal_ot_form_file|crispy}}
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Confirm</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</form>
我的代码请求有什么问题。文件返回 <MultiValueDict: {}>
谢谢
解决方案
推荐阅读
- reactjs - ReactJS componentDidMount 获取单元测试
- php - PHP如何从csv文件的所有列中获取数据并将其存储在数组中
- azure-cognitive-services - 从 PDF 文件的所需页面中提取文本
- indexing - Cosmos 重新索引所需的时间比预期的要长
- swift - 'JSONEncoder' 类型的默认参数值无法转换为'Encoder' 类型(TopLevelEncoder)
- flutter - Cloud Firestore 子集合访问权限
- java - 使“数据”瞬态或可序列化 SonarQube 错误
- html - HTML5 创建隐藏文本的字段,但可以复制/粘贴
- cdap - 元数据/数据沿袭应用程序是否有用于外部数据源的 API?
- reactjs - 不能在导出函数上使用等待?