【问题标题】:A weird Django error一个奇怪的 Django 错误
【发布时间】:2011-11-10 10:55:54
【问题描述】:

这是我的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,其中没有任何表单。但是,它把我带到了一些甚至不存在的联系页面。

错误:

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] "

【问题讨论】:

  • 你有没有检查 urls.py 中的“联系人”条目。
  • 是的。我有。没有接触的痕迹。甚至不在模板中。模板有“.”
  • 您是否检查过“quesdisplay.html”中的“联系人”?在您的警告中不会出现“联系人”。你在混合问题吗?
  • 这样写能解决问题吗:from django.views.decorators.csrf import csrf_exempt from django.template import RequestContext @csrf_exempt def my_function(request) :return render_to_response('html_file', {'some_data' : data}, context_instance=RequestContext(request))
  • 通过使用请求上下文,是的。但是,如果您能解释一下,原因是什么?我似乎经常面对这种错误。而且,为什么在我对表单 action = "" 部分进行任何更改后必须重新启动 django 才能正常工作。

标签: django


【解决方案1】:

如之前的评论中所见,使用 Requestcontext 可以解决您的问题。

这里是关于 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,因为它就像 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)

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 脚本。 似乎对您的情况有帮助:)

希望对你有帮助;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多