Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

A weird Django error

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

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.

Error:

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.

Edit: 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] "
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

As we can read :

Use RequestContext, which always uses 'django.core.context_processors.csrf' (no matter what your TEMPLATE_CONTEXT_PROCESSORS setting). If you are using generic views or contrib apps, you are covered already, since these apps use RequestContext throughout.

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)

or

from django.views.generic.simple import direct_to_template

def app_view(request):             
    return direct_to_template(request, 'app_template.html', app_data_dictionary)

or

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

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

Hope it helps ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...