I must just be overlooking something here, but after stripping everything back - I can't seem to get Django to render either a Form or ModelForm to my template. Code below:
我必须在这里忽略一些东西,但在剥离一切之后 - 我似乎无法让Django将Form或ModelForm呈现给我的模板。代码如下:
#forms.py
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
#views.py
def index(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid(): # All validation rules pass
return HttpResponseRedirect('/thanks/')
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
#new_website_html
<html>
<body>
<form method = "post" action = "">
{{ form.as_p }}
</form>
</body>
</html>
2 个解决方案
#1
1
I had the same issue and after many tries this is my solution:
我有同样的问题,经过多次尝试,这是我的解决方案:
Change the view from this
从中更改视图
#views.py
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
To that:
#views.py
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
Or a simple newline is enough:
或者一个简单的换行就足够了:
#views.py
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
Strange thing is, after these changes the original code worked.
奇怪的是,经过这些修改后,原始代码才起作用。
#2
0
Any error page or just blank page? Actually I just try your code and get form rendering correct(I don't know how to insert local result image here) Please make sure DEBUG=TRUE in settings.py while it's not the problem. @Burhan I think indent problem only happens because he edits it in *. Btw, your form doesn't have a submit button, maybe add it in html like
任何错误页面或只是空白页?实际上我只是尝试你的代码并使表单呈现正确(我不知道如何在这里插入本地结果图像)请确保settings.py中的DEBUG = TRUE,而不是问题。 @Burhan我认为缩进问题只会发生,因为他在*中编辑它。顺便说一句,你的表单没有提交按钮,也许可以像html一样添加它
#1
1
I had the same issue and after many tries this is my solution:
我有同样的问题,经过多次尝试,这是我的解决方案:
Change the view from this
从中更改视图
#views.py
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
To that:
#views.py
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
Or a simple newline is enough:
或者一个简单的换行就足够了:
#views.py
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
Strange thing is, after these changes the original code worked.
奇怪的是,经过这些修改后,原始代码才起作用。
#2
0
Any error page or just blank page? Actually I just try your code and get form rendering correct(I don't know how to insert local result image here) Please make sure DEBUG=TRUE in settings.py while it's not the problem. @Burhan I think indent problem only happens because he edits it in *. Btw, your form doesn't have a submit button, maybe add it in html like
任何错误页面或只是空白页?实际上我只是尝试你的代码并使表单呈现正确(我不知道如何在这里插入本地结果图像)请确保settings.py中的DEBUG = TRUE,而不是问题。 @Burhan我认为缩进问题只会发生,因为他在*中编辑它。顺便说一句,你的表单没有提交按钮,也许可以像html一样添加它