国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

djangoPOST表單的使用

2019-11-14 17:08:49
字體:
來源:轉載
供稿:網友

環境如下:django 1.7.8 版本.

1.在POST表單的時候會出現這個錯誤提示.

禁止訪問 (403)CSRF驗證失敗. 相應中斷.HelpReason given for failure:    CSRF token missing or incorrect.    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.

無耐.看上面的提示是防止CSRF.也就是安全方面的,防止跨站請求偽造.

根據上面的提示來.需要有{% csrf_token %},那就在from表單中添加下

添加后的html代碼如下.

{% extends "base.html" %}{% block title %}    hello{% endblock %}{% block content %}    <div class="container">      <form class="form-signin" action="/login_webmail/" method='post'>{% csrf_token %}        <h2 class="form-signin-heading">Please sign in</h2>        <label class="sr-only" for="inputUserName">Email address/UserName</label>        <input type="text" autofocus="" required="" placeholder="Email address/UserName" class="form-control" id="inputUserName" name="inputUserName">        <label class="sr-only" for="inputPassWord">Password</label>        <input type="password" required="" placeholder="Password" class="form-control" id="inputPassword">        <div class="checkbox">          <label>            <input type="checkbox" value="remember-me"> Remember me          </label>        </div>        <button type="submit" class="btn btn-lg btn-primary btn-block">Sign in</button>      </form>    </div> <!-- /container -->{% endblock %}

重點是from后面的{% csrf_token %}

根據官網的提示及百度.

views.py的代碼更改如下,主要的是return render_to_response('index.html',context_instance=RequestContext(request))

后面的 **context_instance=RequestContext(request)**

 

from django.http import HttpResponseimport datetimefrom django.shortcuts import render_to_response#postfrom django.template import RequestContext#postdef webindex(request):    return render_to_response('index.html',context_instance=RequestContext(request))    

 

接收的views視圖方法

def login_webmail(request):    if 'inputUserName' in request.POST:        message = request.POST['inputUserName']    else:        message = "Not inputUserName"    return render_to_response('test_post.html',{'test_post_name':message})    

再測試.是否OK了.總結.只有兩個步驟.

1.在from 表單中添加 {% csrf_token %}

2.在視圖中添加 from django.template import RequestContext 導入項,并且在return 返回中添加context_instance=RequestContext(request)

然后就OK了.看來也是很簡單的.新手可以參考.

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深水埗区| 高安市| 丹寨县| 利津县| 靖江市| 利川市| 仁化县| 桃园市| 利辛县| 六安市| 云阳县| 芦山县| 开封县| 洪泽县| 珲春市| 榆树市| 浦县| 常州市| 吉林省| 聂荣县| 镇江市| 南京市| 浦城县| 泸州市| 江安县| 永靖县| 宝鸡市| 佛冈县| 凤翔县| 治县。| 广州市| 大新县| 明光市| 阿荣旗| 盖州市| 榆树市| 准格尔旗| 萍乡市| 西华县| 广元市| 潜山县|