文件集不会上传文件

时间:2018-03-19 17:48:51

标签: python django

我有一个表格用于我的Lecture模型和一个formset,它允许我为每个讲座上传多个文件。以前它曾经工作但现在我的文件不再上传了......我无法弄清楚它有什么问题。我的表单有enctype和request.FILES被添加到我的视图..但无论如何都不会保存文件。

FileFormset = inlineformset_factory(Lecture, FileUpload, exclude=[])

class LectureForm(forms.ModelForm):
    lecture_title = forms.CharField(max_length=100, required=True,
                                    widget=forms.TextInput(
                                        attrs={'class': 'lec_title', 'placeholder': 'Lecture Title'}))
    course = forms.ChoiceField(widget=forms.Select(attrs={'class': 'lec_course'}))
 <form style="display: none" method="post" class="form2" action="{% url "classroom" %}"
                              enctype="multipart/form-data">
                            {% csrf_token %}
                            <h5 style="text-align: center; margin-bottom:20px;">ADD LECTURE</h5>
                            {{ form1.course }}<br>
                            {{ form1.lecture_category }}<br>
                            {{ form1.lecture_title }}<br>
                            {{ form1.content }}<br>
                            {{ formset.management_form }}
                            {% for form in formset %}
                                <label id="file" for="file-upload" class="custom-file-upload">
                                    <i class="fas fa-cloud-upload-alt"></i> File Upload
                                </label><br>
                                <span class="selected" id="file-selected"></span>
                                <input id="file-upload" name='files' type="file" style="display:none"/> <br>
                            {% endfor %}
                            <br>
                            <button class="btn btn-default btn2" type="submit">Add</button>
                        </form>
 if 'form1-course' in request.POST:
            form1 = LectureForm(request.POST, user=request.user.teacher, prefix='form1')
            if form1.is_valid():
                lecture = form1.save()
                formset = FileFormset(request.POST, request.FILES, instance=lecture, prefix='files')
                if formset.is_valid():
                    formset.save()
                    form1 = LectureForm(user=request.user.teacher, prefix='form1')
                    messages.success(request, 'Lecture added successfully.')
                formset.save()
        else:
            form1 = LectureForm(user=request.user.teacher, prefix='form1')
            formset = FileFormset()

0 个答案:

没有答案