所讨论的表单根本不会呈现表单字段,尽管没有引发任何异常(我假设这意味着错误不是视图或表单错误)。所以我的问题是,还有什么可能导致这样的错误?
形式:
class HandInForm(forms.ModelForm):
class Meta:
model = AssignmentsHandedIn
fields = ['assignment', 'user_hand_in']
class ResourceForm(forms.ModelForm):
"""Form definition for File."""
class Meta:
"""Meta definition for Fileform."""
model = Resources
fields = ['file']
widgets = {
'file': forms.FileInput(attrs={"class": "form-control input"}),
}
视图:
def hand_in(request, assignment_id):
assignment_actual = Assignments.objects.get(pk=assignment_id)
class_assignment = assignment_actual.class_related
if request.method == "POST":
assignment_form = HandInForm(data=request.POST)
resource_form = ResourceForm(data=request.POST)
if assignment_form.is_valid() and resource_form.is_valid():
assignment = assignment_form.save()
assignment.user_hand_in = request.user
assignment.assignment = assignment_actual
resource = resource_form.save()
resource.assignments = assignment
resource.class_related = class_assignment
else:
print(assignment_form.errors, resource_form.errors)
else:
assignment_form = HandInForm()
resource_form = ResourceForm()
return render(request, "DC/handin.html", {'assignment_form': assignment_form, 'resource_form': resource_form})
模板:
{% extends "DC/base.html" %}
{% block body_block %}
<br><br><br><br>
<h1 class="titley">New Resource</h1>
<form class="jumbotron" enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary" name="button">Create</button>
</form>
<script>
var editor = new MediumEditor('.editable');
</script>
{% endblock body_block %
}