[英]Django views.contact didn't return an HttpResponse object
Hey guys I am running a blog with Django on a server but the contact form is not working, and after messing with it a bit, I broke it but the code is the original so I have no idea why it wouldn't be working. 大家好,我正在服务器上使用Django运行博客,但是联系表单不起作用,在弄乱了一点之后,我将其破坏了,但是代码是原始代码,所以我不知道为什么它不起作用。 Here is my contact form:
这是我的联系表格:
def contact(request):
errors = []
if request.method == 'POST':
if not request.POST.get('subject', ''):
errors.append('Enter a subject.')
if not request.POST.get('message', ''):
errors.append('Enter a message.')
if request.POST.get('email') and '@' not in request.POST['email']:
errors.append('Enter a valid e-mail address.')
if not errors:
send_mail(
request.POST['subject'],
"User's Email: " + request.POST['email'] + " User's Message: " + request.POST['message'],
request.POST.get('email', 'my_email@mail.com'),
['my_email@mail.com'], #email address where message is sent.
)
return HttpResponseRedirect('/thanks/')
return render(request, 'contact.html',
{'errors': errors})
Now I get this error when I run this page in the site: 现在,当我在网站上运行此页面时,我得到此错误:
ValueError at /contact/
The view blog.views.contact didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Any help would be appreciated. 任何帮助,将不胜感激。
can you try 你能试一下吗
return render_to_response(request, 'contact.html',
{'errors': errors})
If you want to redirect at other view, I advice you to use : 如果您想重定向到其他视图,建议您使用:
return redirect(<name of a view>, permanent=True)
instead of HttpResponseRedirecter
代替
HttpResponseRedirecter
So I did what Peter suggested, checking the indentation of the last return statement (needed to not be indented) and yes... that was it. 因此,我按照Peter的建议做了,检查了最后一个return语句的缩进(不需要缩进),是的……就是这样。 It's almost like I knew I was going to overlook something like this :p thanks to Peter and everybody for your help, I really appreciate you guys!!
几乎就像我知道我将忽略这样的事情:谢谢彼得和大家的帮助,我非常感谢你们! Have a good weekend
周末愉快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.