Django csrf 两种方法设置form的实例

yipeiwu_com6年前Python基础

第一种方法,在视图函数上边添加一条语句

@csrf_exempt

例子:

@csrf_exempt
def login(request):
 return render_to_response('app/login.html', locals())

上边的方法是取消csrf的防御机制。

第二种方法,给出例子,主要为在html的form里面加入{% csrf_token %}这句话,视图函数内加入render(request, 'app/breakdown_view.html', locals())

例子:

 <div class="container">
      <form class="form-signin" method="POST">
        {% csrf_token %}
        <h2 class="form-signin-heading">Please login in</h2>
        <label for="inputEmail" class="sr-only">Email address</label>
        <input name="loginEmail" type="email" id="inputEmail" class="form-control" placeholder="Email address" value = "{{myLogin.loginEmail}}" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input name="loginPassword" type="password" id="inputPassword" class="form-control" placeholder="Password" required>
        <!-- <div class="checkbox">
          <label>
            <input type="checkbox" value="remember-me"> Remember me
          </label>
        </div> -->
        <button class="btn btn-lg btn-primary btn-block" type="submit">Login in</button>
      </form>
    </div> <!-- /container -->
def login(request):
	return render(request, 'app/login.html', locals())

以上这篇Django csrf 两种方法设置form的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pygame游戏之旅 添加游戏介绍

pygame游戏之旅 添加游戏介绍

本文为大家分享了pygame游戏之旅的第9篇,供大家参考,具体内容如下 在游戏开始之前定义一个函数,用来显示游戏介绍: def game_intro(): intro = Tru...

python求素数示例分享

复制代码 代码如下:# 判断是否是素数def is_sushu(num): res=True for x in range(2,num-1):  ...

python实现图片批量压缩程序

 本文实例为大家分享了python实现图片批量压缩程序的具体代码,供大家参考,具体内容如下 说明 运行环境:Win10 Pycharm 程序没有用到面向对象编程方法,...

TensorFlow车牌识别完整版代码(含车牌数据集)

TensorFlow车牌识别完整版代码(含车牌数据集)

在之前发布的一篇博文《MNIST数据集实现车牌识别--初步演示版》中,我们演示了如何使用TensorFlow进行车牌识别,但是,当时采用的数据集是MNIST数字手写体,只能分类0-9共1...

python 循环数据赋值实例

python在数值赋值的时候可以采用数值内循环赋值,很方便 如下 a = [x for x in range(10)] 这样 a = [0,1,2,3,4,5,6,7,8,9]...