gpt4 book ai didi

python - django csrf_token 不打印隐藏的输入字段

转载 作者:太空狗 更新时间:2023-10-29 21:38:03 25 4
gpt4 key购买 nike

我的views.py:

from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
from django.http import *
from django.template import *
from django.shortcuts import *
# Create your views here.
@csrf_protect
def homepage(request):
return render_to_response('index.html', {'files':os.listdir('/home/username/public_html/posters') })
@csrf_protect
def upload(request):
return render_to_response('list.html', )

在我的模板 index.html 中:

<html>
<body>
<h1> All uploaded posters: </h1>
<form action='/posters/upload' method= 'POST'>{%csrf_token%}
<input type='file' name= 'uploadfile'>Upload new poster <input type="submit" value = "Upload">
</form>
{%for file in files %}
<a href = 'http://servername/~username/posters/{{file}}'>{{file}}</a> <br />
{%endfor%}
</body>
</html>

所以当我在浏览器中打开主页并查看源代码时,没有 csrf token !

<html>
<body>
<h1> All uploaded posters: </h1>
<form action='/posters/upload' method= 'POST'>
<input type='file' name= 'uploadfile'>Upload new poster <input type="submit" value = "Upload">
</form>

<a href= ......

我错过了什么?

更新:this帮助。

最佳答案

您需要使用 RequestContext 才能使用 CSRF 中间件:

from django.template import RequestContext

# In your view:
return render_to_response('index.html'
{'files':os.listdir('/home/username/public_html/posters') },
context_instance=RequestContext(request))

顺便说一句:不推荐使用 csrf_protect 装饰器,因为如果您忘记使用它,就会出现安全漏洞。

关于python - django csrf_token 不打印隐藏的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9247669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com