第一种方法,在视图函数上边添加一条语句
1
|
@csrf_exempt |
例子:
1
2
3
|
@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())
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<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 - - > |
1
2
|
def login(request): return render(request, 'app/login.html' , locals ()) |
以上这篇Django csrf 两种方法设置form的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u013023781/article/details/61617589