一个奇怪的Django错误 [英] A weird Django error

查看:247
本文介绍了一个奇怪的Django错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的views.py:

This is 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})

因此,当我点击提交按钮时,它应该显示quesdisplay.html,没有任何形式。但是,它正在带我到一些甚至不存在的联系页面。

Thus, when I click on submit button, it should show the quesdisplay.html without any form in it. But, it is taking me to some contact page which doesn't even exist.

错误:

The current URL, contact/, didn't match any of these.

我已经尝试过所有可能的方法来调试这个,但它不可能,因为没有任何痕迹这个叫做联系。

I've tried all possible ways to debug this but its not possible as there is no trace of anything called "contact" in this.

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

This is the warning I get:

/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解决您的问题。

As seen in the comment before, using Requestcontext solve your problem.

以下是有关csrf_token的文档: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it

Here is the documentation about csrf_token : https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it

我们可以看到:


使用RequestContext,其中总是使用
'django.core.context_processors.csrf'(无论你的
TEMPLATE_CONTEXT_PROCESSORS设置如何)。如果您使用通用视图
或contrib应用程序,因为这些应用程序使用
RequestContext。

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

So here it seems we're not using a generic view, neither a contrib app. So what we need it to pass the RequestContext because it's like that csrf protection works in 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)

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))

同样的文档说明:extras / csrf_migration_helper.py脚本。
似乎对你的情况有帮助:)

Also the documentation speaks about : extras/csrf_migration_helper.py script. Seems to be helpful for your case :)

希望它有帮助;)

这篇关于一个奇怪的Django错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆