Django 403 CSRF令牌丢失或不正确 [英] Django 403 CSRF token missing or incorrect

查看:393
本文介绍了Django 403 CSRF令牌丢失或不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这个问题,但不幸的是仍然不知道如何解决它。该表单完美呈现,我输入信息并获得CSRF错误。给出的原因是令牌丢失或错误

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.

查看:

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

这是我的模板:

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

我认为我做的一切正确,我不知道是什么导致了CSRF错误。另外,如果它的相关性我跟随这个作为指导:

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

推荐答案

这个堆栈在这里 Django视图没有 t返回一个HttpResponse对象。能够帮助我整理出来。一旦我向我的 render_to_response 中添加了 context_instance = RequestContext(request)

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.

这篇关于Django 403 CSRF令牌丢失或不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆