gpt4 book ai didi

python - Django : CSRF verification failed even after adding {% csrf_token %}

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:38 31 4
gpt4 key购买 nike

views.py:

def index(request):
return render_to_response('index.html', {})

def photos(request, artist):
if not artist:
return render_to_response('photos.html', {'error' : 'no artist supplied'})
photos = get_photos_for_artist(artist)
if not photos:
logging.error('Issue while getting photos for artist')
return render_to_response('photos.html', {'error': 'no matching artist found'})
return render_to_response('photos.html', {'photos': photos})

Index.html:

<html>
<head>
<title>find artist photos </title>
</head>
<body>
{% block error %} {% endblock %}
<form action="/photos" method="POST">
{% csrf_token %}
<label for="artist">Artist : </label>
<input type="text" name="artist">
<input type="submit" value="Search">
</form>
{% block content %}{% endblock %}
</body>
</html>

照片.html:

{% extends 'index.html' %}
{% block error %}
{% if error %}
<p> {{ error}} </p>
{% endif %}
{% endblock %}

{% block content %}
{% if photos %}
{% for photo in photos %}
{{ photo }}
{% endfor %}
{% endif %}
{% endblock%}

url.py:

urlpatterns = patterns('',
(r'', index),
(r'^time/$', current_datetime),
(r'^photos/(\w+)$', photos)
)

我什至尝试添加 {% csrf_token %},但没有成功

谢谢

更新
我在日志中看到了这些

UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value.  This is usually caused by not using RequestContext.
warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.")

这是在添加 context_instance=RequestContext(request) **到 render_to_response()** 之后出现的

最佳答案

context_instance=RequestContext(request) 添加到您将在其中使用表单的每个 View :

return render_to_response('index.html', {}, context_instance=RequestContext(request) )


return render_to_response('photos.html', {'photos': photos}, context_instance=RequestContext(request) )

关于python - Django : CSRF verification failed even after adding {% csrf_token %},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8408545/

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