簡體   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

該請求永遠不會發布。 當我在表單上單擊“提交”時,它永遠不會顯示為發布請求。 我可能做錯了什么?


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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM