简体   繁体   中英

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. I am unable to figure out why ? I pasted contact.html and forms.py for more visibility. EDIT: I have implemented all the changes suggested but the form never renders the post method. 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. 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.

However, second if statement checks if form is valid and this is the line where things get south. 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. If you don't want form tags included, set the form_tag attribute for your form helper to False .

self.helper.form_tag = False

You need not explicitly use {% csrftoken %} tag, crispy adds that for you.

Also I don't see that you're using crispy form helper in your forms.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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