奇怪的Django错误

奇怪的Django错误,django,Django,这是my views.py: # Create your views here. from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from django.db import models from display.forms import CodeForm from display.forms import CodeFormSet

这是my views.py:

# Create your views here.

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.db import models

    from display.forms import CodeForm
    from display.forms import CodeFormSet
    from ExamPy.questions.models import QuestionBase


    def codepost(request):
        if request.method == 'POST':
            form = CodeFormSet(request.POST)
            if form.is_valid():
                titles = []
                for i in range(0, self.total_form_count()):
                            form = self.forms[i]
                            title = form.cleaned_data['title']
                            if title in titles:
                                raise forms.ValidationError("Articles in a set must have distinct titles.")
                                titles.append(title)
                return render_to_response('quesdisplay.html')
        else:
            form = CodeFormSet()

        return render_to_response('quesdisplay.html', {'form':form})
因此,当我单击submit按钮时,它应该显示quesdisplay.html,其中没有任何表单。但是,它把我带到了一个根本不存在的联系页面

错误:

The current URL, contact/, didn't match any of these.
我已经尝试了所有可能的方法来调试它,但这是不可能的,因为这里没有任何所谓的“联系人”的痕迹

编辑: 这是我得到的警告:

/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py:101: 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.")
[10/Nov/2011 05:34:17] "

正如前面的评论所示,使用Requestcontext可以解决您的问题

以下是有关csrf_令牌的文档:

正如我们所看到的:

使用RequestContext,它始终使用 'django.core.context_processors.csrf'(无论您的 模板\上下文\处理器设置)如果您使用的是常规视图 或contrib应用程序,由于这些应用程序使用 请求整个上下文。

因此,这里似乎我们没有使用通用视图,也没有使用contrib应用程序。 所以我们需要它来传递RequestContext,因为它就像csrf保护在Django中工作一样

from django.core.context_processors import csrf
from django.shortcuts import render_to_response

def my_view(request):
    c = {}
    c.update(csrf(request))
    # ... view code here
    return render_to_response("a_template.html", c)

文档中还提到:extras/csrf\u migration\u helper.py脚本。 似乎对您的案例有帮助:)


希望有帮助;)

您是否检查了URL.py.Yes中的“联系人”条目。我有。没有接触的痕迹。甚至在模板中也没有。模板有“.”是否已选中“quesdisplay.html”中的“联系人”?在您的警告中,不显示“联系人”。你在混合问题吗?如果你写:
from django.views.decorators.csrf import csrf\u employ from django.template import RequestContext@csrf\u employ def my\u function(request):使用请求上下文返回render\u to\u response('html\u file',{'some\u data':data},context\u instance=RequestContext(request))
,是的话。但如果你能解释,原因是什么?我似乎经常面对这种错误。还有,为什么我必须在对表单action=”“部分进行任何更改后重新启动django才能使其工作。
from django.views.generic.simple import direct_to_template

def app_view(request):             
    return direct_to_template(request, 'app_template.html', app_data_dictionary)
from django.shortcuts import render_to_response
from django.template import RequestContext

def app_view(request):
    return render_to_response('app_template.html', 
                              app_data_dictionary, 
                              context_instance=RequestContext(request))