Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django 1.10中制作表单并收到CSRF验证失败。请求中止_Python_Html_Django_Forms_Django Forms - Fatal编程技术网

Python 在Django 1.10中制作表单并收到CSRF验证失败。请求中止

Python 在Django 1.10中制作表单并收到CSRF验证失败。请求中止,python,html,django,forms,django-forms,Python,Html,Django,Forms,Django Forms,我很难使用Django获得POST请求。我不断收到403错误消息: 我试图最终制作一个搜索栏来搜索数据库。在index.html上我有一个搜索栏,然后它接受用户输入并将结果发送到search.html页面。我还没有实现查询搜索,我只是希望使用Django表单功能将用户输入显示在search.html页面上 我一直在使用本教程制作我的第一个表单: 下面是我创建的python和html脚本(一些变量与教程略有不同,但概念相同) index.html <form action="search

我很难使用Django获得POST请求。我不断收到403错误消息:

我试图最终制作一个搜索栏来搜索数据库。在index.html上我有一个搜索栏,然后它接受用户输入并将结果发送到search.html页面。我还没有实现查询搜索,我只是希望使用Django表单功能将用户输入显示在search.html页面上

我一直在使用本教程制作我的第一个表单:

下面是我创建的python和html脚本(一些变量与教程略有不同,但概念相同)

index.html

<form action="search/" method="post">
        <div class="search-bar">
            <input name="search" type="text" class="form-control" placeholder="What are you interested in?" style="width:1000px"> &nbsp; 
            <button type="submit" class="btn btn-default text-center" style="align-items: center;">Search</button>
        </div>
    </form>
url.py

from django.conf.urls import url

from . import views

app_name = 'ahawebsite'
urlpatterns = [

# Look to views.py file to see the references of the [ ]View objects
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'about/$', views.AboutView.as_view(), name='about'),
    url(r'contact/$', views.ContactView.as_view(), name='contact'),
    url(r'signup/$', views.SignupView.as_view(), name='signup'),
    url(r'search/$', views.get_name, name='search'),
    url(r'^thanks/$',views.say_thanks,name='thanks'),
]
search.html

<form action = "/search/" method = "post">
    {% csrf_token %}
    {{ form }}
    <input type = "submit" value = "Submit" />
</form>

{%csrf_令牌%}
{{form}}

在使用Django表单功能时,我不希望得到403页作为我的响应。我是否正确使用CSRF令牌系统?还是我做了什么完全错误的事?

您在
index.html
中的表单没有
{%csrf\u token%}
。但它仍然会向需要它的函数发布(
get\u name
)。注意,如果
index.html
中的表单应该发布到
/search/
,那么您需要在表单中添加
{%csrf\u token%}
,因为
get\u name
受csrf保护。

我将{%csrf\u token%}放在index.html文件中,效果非常好!现在我可以开始使用数据库搜索功能了。谢谢我想回答你的问题,你应该接受它作为答案,让别人知道。
<form action = "/search/" method = "post">
    {% csrf_token %}
    {{ form }}
    <input type = "submit" value = "Submit" />
</form>