Py学习  »  Django

Django提交表单报错!求解决

415293799 • 8 年前 • 5088 次点击  

前端部分:

{% extends 'index.html' %}
{% block content_1 %}
    <div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <form class="form-search" method="POST">
            {% csrf_token %}
                <input class="input-medium search-query" type="text" name="keyword" />
                    <button type="submit" class="btn">查找</button>

            <table class="table table-bordered table-hover table-condensed">
        。。。。。。      
                    {% if keyname %}
                        {% print keyname %}
                        {{ keyname }}
                    {% else %}
                    {% for info in server_list %}

                    {% endfor %}
                 。。。。。。。
                      {% endif %}
                </tbody>
            </table>
            <p>
            每日总费用:{{ all_prices }} 美元
            </p>
            </form>
        </div>
    </div>
</div>
{% endblock %}

url.py部分:

url(r'^search/',views.search,name="keyword"),

view:

def search(request):
   if "keyword" in request.POST:
        keyword = request['keyword']
        if not keyword:
            error = 'xxxx'
        else:
            return render_to_response('sfasset.html',{
                                'keyname' : keyword,})
   return render_to_response('sfasset.html',{'error': error})

报错:

Forbidden (403)
CSRF verification failed. Request aborted.
You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.
If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for 'same-origin' requests.
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 passes a request to the template's render method.
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.
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1273
 
5088 次点击  
文章 [ 2 ]  |  最新文章 8 年前
415293799
Reply   •   1 楼
415293799    8 年前

@MCC 多谢

MCC
Reply   •   2 楼
MCC    8 年前

http://www.django-china.cn/topic/1239/?page=1