有问题的表单尽管没有引发任何异常(我认为这意味着该错误不是视图或表单错误),但根本不会呈现表单字段。所以我的问题是,还有什么可能导致这种错误? 形式:
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 %
}
答案 0 :(得分:1)
那是因为您分别为表单HandInForm()
ResourceForm()
命名:
assignment_form = HandInForm()
resource_form = ResourceForm()
但是,您在模板中使用名称form
{{ form.as_p }}
改为使用:
{{ assignment_form.as_p }}
{{ resource_form.as_p }}
答案 1 :(得分:0)
您的视图使用不同的名称发送表单。
{'assignment_form': assignment_form, 'resource_form': resource_form}
但是您使用form
来调用表单。您应该使用定义的名称。喜欢
{{ assignment_form.as_p }}
,{{ resource_form.as_p }}