![](/img/trans.png)
[英]request.method == 'POST' is not working in Django
[英]if request.method =='POST': is always failing
#Contact.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class='row'>
<div class ='col-md-4 col-md-offset-4'>
<h1> {{title}} </h1>
{% if confirm_message %}
<p>{{ confirm_message }}</p>
{% endif %}
{% if form %}
<form method='POST' action=''>
{% csrf_token %}
{{ form.errors }}
{{ form.non_field_errors }}
{% crispy form %}
<input type='submit' value='submit form' class='btn btn-default' />
</form>
{% endif %}
</div>
</div>
{% endblock %}
# froms.py
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field
from crispy_forms.bootstrap import (PrependedText, PrependedAppendedText, FormActions)
class contactForm(forms.Form):
name = forms.CharField(required = False , max_length =100, help_text="100 characters max ")
email= forms.EmailField(required = True)
comment = forms.CharField(required =True, widget=forms.Textarea)
Server Logs
System check identified no issues (0 silenced).
September 13, 2017 - 07:38:19
Django version 1.11.5, using settings 'app3.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
GET
hello from not valid
[13/Sep/2017 07:38:23] "GET /contact/ HTTP/1.1" 200 5413
[13/Sep/2017 07:42:20] "GET / HTTP/1.1" 200 4356
[13/Sep/2017 07:42:27] "GET /about/ HTTP/1.1" 200 3985
GET
hello from not valid
[13/Sep/2017 07:42:37] "GET /contact/ HTTP/1.1" 200 5413
该请求永远不会发布。 当我在表单上单击“提交”时,它永远不会显示为发布请求。 我可能做错了什么?
#Views page
from django.shortcuts import render
from .forms import contactForm
from django.conf import settings
from django.core.mail import send_mail
def contact(request):
form = contactForm()
confirm_message = None
title = 'Contact'
context ={'title' : title, 'form' : form }
print request.method
# print form
# if request.method=='GET':
# form = contactForm()
if request.method =='POST':
form = contactForm(request.POST )
print "hello from not valid "
if form.is_valid():
print "hello"
name = form.cleaned_data['name']
comment=form.cleaned_data['comment']
emailFrom=form.cleaned_data['email']
subject ='Message from mysite.com'
message='%s %s' %(comment, name )
emailTo=[settings.EMAIL_HOST_USER]
title = 'Thank you'
confirm_message="Thank you, we ll get back to you "
context ={'title' : title,'confirm_message' :
confirm_message}
template ='contact.html'
return render(request , template , context)
这是我的视图页面,处理该应用程序的所有业务逻辑。当我运行该应用程序时,代码永远不会到达request == post块。 我不知道为什么? 我粘贴了contact.html和forms.py以获得更多可见性。 编辑:我已经实现了建议的所有更改,但该窗体从不呈现post方法。 我可以说形式有问题,但我不知道该怎么办。
UPDATE2:该问题已解决,并且该问题看起来像是松脆的。 我阅读了文档,除了将请求呈现为帖子的事实之外,找不到其他可以指出错误的地方。 决定将其删除,现在可以正常使用了。 谢谢大家的帮助和建议。
您可以在服务器日志中看到“来自无效的hello”字符串,这意味着您的POST请求已成功发送到服务器。
但是,第二条if语句检查表单是否有效,这是事情发展的方向。 由于没有其他形式的无效表单,因此无法看到正确的错误消息。
修正表格并涵盖无效的情况。
在模板中,您的表单构造错误。
如果您在模板中使用{% crispy %} tag
,它将形成一个表单。 如果您不希望包含表单标签,请将表单助手的form_tag
属性设置为False
。
self.helper.form_tag = False
您无需显式使用{% csrftoken %}
标记,酥脆的为您添加。
另外,我看不到您在forms.py
使用了酥脆的表单助手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.