什么解决方案'CSRF验证失败。请求中止。'错误

时间:2014-05-30 18:10:00

标签: mysql django python-2.7

我正在尝试制作简单的登录应用程序,因此代码如下所示。但这些代码出错,所以我试图解决此错误。

views.py

 def login_user(request):
    state = "Please log in below..."
    username = password = ''
    if request.POST:
        username = request.POST.get('user_id')
        password = request.POST.get('pass_id')

        user = login(user_name=username, password=password)
        if user is not None:
        i f user.is_active:
            login(request, user)
            state = "You're successfully logged in!"
        else:
            state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

return render_to_response('auth.html', {'state': state, 'username': username})

models.py

from django.db import models
class login(models.Model):

user_name = models.CharField(max_length=400)
password = models.CharField(max_length=400)

def __str__(self):
    return ' '.join([
        self. ordering,

    ])

auth.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
 <title>Log in</title>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
 <style>
 body{
font-family:Arial,Helvetica,sans-serif;
font-size: 12px;
  }
  </style>
  </head>
  <body>
{{ state }}
<form action="/login/" method="POST">
    {% csrf_token %}
    {% if next %}
    <input type="hidden" name="next" value="{{ next }}" />
    {% endif %}
    username:
    <input type="text" name="username" value="{{ username}}" /><br />
    password:
    <input type="password" name="password" value="" /><br />

    <input type="submit" value="Log In" />
</form>
</body>
</html>

urls.py

urlpatterns = patterns('',
# Examples:

url(r'^login/$',  login_user),

)

但我收到了错误:

 Forbidden (403)

 CSRF verification failed. Request aborted.

那么我能解决这个问题怎么办?任何人都可以为这段代码提供帮助吗?

0 个答案:

没有答案