python – 避免django 1.4.3中的csrf错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 避免django 1.4.3中的csrf错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4739字,纯文字阅读大概需要7分钟。
内容图文
![python – 避免django 1.4.3中的csrf错误](/upload/InfoBanner/zyjiaocheng/816/a729808bd9a14a068b7f730926b0279b.jpg)
我正在使用django设计一个基本的登录和注销页面.以下是我的代码
settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
...........
...........
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
"django.core.context_processors.csrf",
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
INSTALLED_APPS = (
'django.contrib.auth',
.......
.......
)
urls.py
from django.conf.urls.defaults import *
from django.conf import settings
urlpatterns = patterns('',
url(r'^$', 'learn_django.views.home_page'),
url(r'^login/$', 'learn_django.views.login'),
url(r'^logged_in$', 'learn_django.views.logged_in'),
url(r'^logout/$', 'learn_django.views.logout'),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + urlpatterns
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
def home_page(request):
return render_to_response("home_page.html")
def login(request):
return render_to_response("login.html")
def logged_in(request):
return render_to_response("logged_in.html",context_instance=RequestContext(request))
base.html文件
{% load staticfiles %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="{% static 'css/home_remaining.css' %}" type="text/css">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<header>
<div class='header_div'>
<div class="logout"><p id='logout'><a href="/logout" >Logout</a></p><div>
<div class="login"><p id='login'> <a href="/login" >Login</a></p><div>
</div>
</header>
<div class="body_content">
{% block body %}{% endblock %}
</div>
</body>
</html>
的login.html
{% extends 'base.html' %}
{% block title %}Login Page{% endblock %}
{% block body %}
<div id='container'>
<form action="/logged_in" method="POST">
{% csrf_token %}
<label for="name">Username:</label><input type="name">
<label for="username">Password:</label><input type="password">
<div id="lower">
<input type="submit" value="Login">
</div>
</form>
</div>
{% endblock %}
所以上面是我的complet代码,当我们点击base.html中给出的Login链接时,它会显示一个登录表单.
登录显示并输入一些用户名和密码并单击“登录”按钮后,将显示一个指示csrf错误的错误页面
用Google搜索并在表单标记中添加{%csrf_token%},还在settings.py中的模板上下文进程中添加了django.core.context_processors.csrf
所以下面是错误消息的样子
Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF cookie not set.
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
Your browser is accepting cookies.
The view function uses RequestContext for the template, instead of Context.
In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.
所以当我从模板上下文进程中删除django.core.context_processors.csrf时,它的工作正常.但我想也使用csrf保护.
最后,实际上在上面的视图代码中出现了什么错误以及为什么出现csrf错误页面以及如何避免上述错误页面?
我是否需要在views.py函数中添加任何代码?
任何人都可以在上面的函数中添加基本的登录和注销功能代码,这样对于实际理解代码会更有帮助……
编辑
对于上面的问题,我导入了csrf_exempt函数,如下所示
来自django.views.decorators.csrf import csrf_exempt
并且在login_in视图之前将其作为装饰器给出,并且当我单击登录按钮时它没有显示错误页面
但仍然想知道为什么下面提到的方法,如从模板发送Requestcontext不起作用
解决方法:
您需要将RequestContext传递给render_to_response函数.
def home_page(request):
return render_to_response("home_page.html", context_instance=RequestContext(request))
或者使用新的render函数,它处理为你传递RequestContext.
def home_page(request):
return render(request, "home_page.html")
RequestContext将各种有用的东西添加到传递给模板的上下文字典中.这包括csrf令牌.有关更多信息,请查看RequestContext docs.
在您的情况下,您的登录视图将呈现login.html模板,但不会传递csrf令牌.当login.html模板回发到服务器(到/ logged_in)时,logged_in视图会检查该csrf令牌.它不存在(因为你从未包括它).所以它假定它收到了一个跨站点请求伪造.
阅读csrf docs以了解更多流程.
内容总结
以上是互联网集市为您收集整理的python – 避免django 1.4.3中的csrf错误全部内容,希望文章能够帮你解决python – 避免django 1.4.3中的csrf错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。