CSRF验证失败。请求中止(Django中的初学者)

时间:2022-06-02 00:22:56

Don't judge me strictly. I'm the total beginner in Django. Here is my problem: I have a form with registration. When I push button 'Submit', here is the CSRF error. I tried to debug it, but I can't understand why the condition if request.method == 'POST' is not true? here is my view method:

不要严格评判我。我是Django的初学者。这是我的问题:我有一个注册表格。当我按下“提交”按钮时,这是CSRF错误。我试着调试它,但我无法理解为什么条件如果request.method =='POST'不正确?这是我的查看方法:

def Logging(request):
    if request.method == 'POST':
        form = Login_Form(request.POST)

        if form.is_valid():
            return HttpResponseRedirect('thanks.html')
    else:
        form = Login_Form()

    return render_to_response('login.html', {'form':form})

and my form:

和我的形式:

<form method = "post">
    <table>
    {{form.as_table}}
    <input type = "submit" value="Login">
      </table>
</form>

and urls:

和网址:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/', views.Logging),
    url(r'^thanks/', 'thanks.html')
)

1 个解决方案

#1


0  

Add {% csrf_token %} inside form tag

在表单标记内添加{%csrf_token%}

<form method = "post">
    {% csrf_token %}
    <table>
    {{form.as_table}}
    <input type = "submit" value="Login">
    </table>
</form>

#1


0  

Add {% csrf_token %} inside form tag

在表单标记内添加{%csrf_token%}

<form method = "post">
    {% csrf_token %}
    <table>
    {{form.as_table}}
    <input type = "submit" value="Login">
    </table>
</form>