简体   繁体   English

如果request.method =='POST':总是失败

[英]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

The request never becomes post. 该请求永远不会发布。 When I hit submit on the form it never shows up as post request. 当我在表单上单击“提交”时,它永远不会显示为发布请求。 What could possibly I be doing wrong ? 我可能做错了什么?


#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)

This is my views page handling all the business logic for the application When I run this application, the code never reaches the request==post block. 这是我的视图页面,处理该应用程序的所有业务逻辑。当我运行该应用程序时,代码永远不会到达request == post块。 I am unable to figure out why ? 我不知道为什么? I pasted contact.html and forms.py for more visibility. 我粘贴了contact.html和forms.py以获得更多可见性。 EDIT: I have implemented all the changes suggested but the form never renders the post method. 编辑:我已经实现了建议的所有更改,但该窗体从不呈现post方法。 I could say something wrong with form but I don't know what. 我可以说形式有问题,但我不知道该怎么办。

UPDATE2: The issue has been resolved and the problem seems to crispy forms. UPDATE2:该问题已解决,并且该问题看起来像是松脆的。 I read the documentation and couldn't find anything to pin point the error besides the fact that it was rendering the request as post. 我阅读了文档,除了将请求呈现为帖子的事实之外,找不到其他可以指出错误的地方。 Decided to remove it and now it works perfectly fine. 决定将其删除,现在可以正常使用了。 Thank you all for your help and suggestions. 谢谢大家的帮助和建议。

You can see "hello from not valid" string in your server log that means your POST request is successfully sended to server. 您可以在服务器日志中看到“来自无效的hello”字符串,这意味着您的POST请求已成功发送到服务器。

However, second if statement checks if form is valid and this is the line where things get south. 但是,第二条if语句检查表单是否有效,这是事情发展的方向。 Since you do not have else case for not valid form, you cannot see the right error message. 由于没有其他形式的无效表单,因此无法看到正确的错误消息。

Fix your form and cover the not valid case. 修正表格并涵盖无效的情况。

In your template your form is constructed wrongly. 在模板中,您的表单构造错误。

If you use {% crispy %} tag in your template, it makes a form. 如果您在模板中使用{% crispy %} tag ,它将形成一个表单。 If you don't want form tags included, set the form_tag attribute for your form helper to False . 如果您不希望包含表单标签,请将表单助手的form_tag属性设置为False

self.helper.form_tag = False

You need not explicitly use {% csrftoken %} tag, crispy adds that for you. 您无需显式使用{% csrftoken %}标记,酥脆的为您添加。

Also I don't see that you're using crispy form helper in your forms.py . 另外,我看不到您在forms.py使用了酥脆的表单助手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM