InfinteScroll
InfinteScroll

Reputation: 684

"CSRF token missing or incorrect"

EDIT SOLVED: I feel like such a moron. I originally created two separate views: one for input and one for output. I had been handling all of the code here in the output view even though I rearranged my url and template to just reuse the input view... SORRY EVERYONE. It works fine now...


I'm running on localhost, and when I submit my form I'm getting a "CSRF token missing" error. I've read the documentation and some stackoverflows. I have already solved most of the common issues:

Anyone have any idea what I could be missing?

Here is my form:

<form action="" method="post">
            {% csrf_token %}
            <input type="text" name="q">
            <input type="submit" value = "Submit">
        </form>

Here is my view:

def view_workout(request):
    errors = []
    if request.method == 'POST':
        q = request.POST['q']
        if not request.POST.get('q', ''):
            errors.append('Please complete all required fields')
            return render_to_response('swimsets/view_workout.html',{
                'error': errors
            },context_instance=RequestContext(request))
        else:
            return render_to_response('swimsets/view_workout.html',{
                'query': q
            },context_instance=RequestContext(request))

    else:

        return render_to_response('swimsets/view_workout.html', {

    },context_instance=RequestContext(request))

Here is my settings:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

Upvotes: 3

Views: 1317

Answers (2)

Michael Burns
Michael Burns

Reputation: 628

You also may need to import csrf into your view:

from django.core.context_processors import csrf

Upvotes: 1

Heikki Toivonen
Heikki Toivonen

Reputation: 31130

You might be missing CsrfResponseMiddleware, based on an answer in related question Is the {% csrf_token %} CSRF protection tag still necessary in Django 1.2?

Upvotes: 1

Related Questions