Search code examples
djangocsrf

Django 403 CSRF token missing or incorrect


I've encountered this issue but unfortunately still do not know how to fix it. The form renders perfectly, I enter the info and get a CSRF error. The reason given is token missing or incorrect.

View:

def eventSell(request, id):
    c = {}
    c.update(csrf(request))
    event = SquidEvent.objects.get(pk = id)
    listing_form = ListingForm(request.POST)
    if request.user.is_authenticated():
        if request.method == 'POST':
            listing_form = ListingForm(request.POST)
            if listing_form.is_valid():
                cd = listing_form.cleaned_data
                user = request.user
                item = Object(price = cd['price'], seller = user)
                item.save()
                return HttpResponseRedirect(reverse('tixeng:index'), c)
            #print listing_form
        else:
            return render_to_response('tixeng/list.html', {'event' : event, 'form' : listing_form}, c)
    else:
        return HttpResponseRedirect(reverse('allauth.account.views.login'))

Here is my template:

<form action="{% url 'app:eventSell' event.id %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>

I think I've done everything right, I'm not sure what's causing the CSRF error. Also, in case its relevant I was following along with this as a guide:

http://www.djangobook.com/en/2.0/chapter07.html


Solution

  • This stack here Django "The view didn't return an HttpResponse object." was able to help me sort it out. Once I added context_instance = RequestContext(request) to my render_to_response it worked.