姜戈,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">&times;</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: {}>

谢谢

标签: django

解决方案


推荐阅读