[英]Django form not showing except for submit button
我遇到了 django 表单未显示在网页上的问题。 唯一显示的是提交按钮。 我无法弄清楚这个问题。
视图.py
class NewThreadView(CreateView):
model = Thread
form_class = NewThreadForm
template_name = 'thread/newthread.html'
def get_context_data(self, *args, **kwargs):
context = super(NewThreadView, self).get_context_data(*args, **kwargs)
forms.py
class NewThreadForm(forms.ModelForm):
class Meta:
model = Thread
fields = ('name', 'body', 'author', 'thread_forum')
widgets = {
'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter title'}),
'body': forms.Textarea(attrs={'class': 'form-control'}),
'author': forms.TextInput(attrs={'class': 'form-control', 'value': '', 'id': 'author', 'type': 'hidden'}),
'thread_forum': forms.Select(attrs={'class': 'form-control', 'type': 'hidden'}),
}
新线程.html
<div class="form-group">
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary" name="thread_submit">Create Thread</button>
</form>
</div>
您的上下文未返回,因此模板无法呈现任何内容。 您需要返回上下文;
class NewThreadView(CreateView):
model = Thread
form_class = NewThreadForm
template_name = 'thread/newthread.html'
def get_context_data(self, *args, **kwargs):
context = super(NewThreadView, self).get_context_data(*args, **kwargs)
return context
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.