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>
photos.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()**之后
小智 9
添加context_instance=RequestContext(request)到您将在其中使用表单的每个视图:
return render_to_response('index.html', {}, context_instance=RequestContext(request) )
return render_to_response('photos.html', {'photos': photos}, context_instance=RequestContext(request) )
假设您使用的是相当新版本的Django(1.3/1.4/dev),您应该按照以下步骤操作:
settings.py,将中间件添加django.middleware.csrf.CsrfViewMiddleware到 
 MIDDLEWARE_CLASSES列表中.{% crsf_token %}表单中的内容.django.core.context_processors.csrf通过以下方式使用上下文处理器:
RequestContext来自django.templatefrom django.core.context_processorsfrom django.template import RequestContext
from django.shortcuts import render_to_response
def my_view(request):
    return render_to_response('my_template.html', {}, context_instance=RequestContext(request))
要么
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
def my_view(request):
    c = {csrf(request)}
    return render_to_response('my_template.html', c)
(后人和未来观众的详尽帖子)
| 归档时间: | 
 | 
| 查看次数: | 8500 次 | 
| 最近记录: |