-1

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] "
Hick
  • 35,524
  • 46
  • 151
  • 243
  • Have you check for 'contact' entry in urls.py. – dani herrera Nov 10 '11 at 10:58
  • Yes. I've. No trace of contact. Not even in the template. Template has "." – Hick Nov 10 '11 at 11:10
  • Have you checked 'quesdisplay.html' for a 'contact'? In your warning not appear 'contact'. are you mixing issues? – dani herrera Nov 10 '11 at 11:17
  • Does it solve the problem if you write : `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))` – Guillaume Cisco Nov 10 '11 at 11:36
  • By using Request Context, yes. But what is the reason if you can explain? I seem to face this kind of error a lot. And, why do I have to restart django after I make any changes to the form action = "" part for it to work. – Hick Nov 10 '11 at 11:52
  • In fact I've dealed with this error a long time ago and don't really remember why doing this solve the problem, so I posted a comment ^^" I will try to understand why it is acting like this and make a real answer. It's gonna be good for me too ;) – Guillaume Cisco Nov 10 '11 at 11:57

1 Answers1

3

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

mattbasta
  • 13,492
  • 9
  • 47
  • 68
Guillaume Cisco
  • 2,859
  • 24
  • 25