开发者

Django: Still getting CSRF error after trying tons of fixes

开发者 https://www.devze.com 2023-03-05 14:02 出处:网络
I am getting \"CSRF token missing or incorrect\". I already checked Stack Overflow for an answer and nothing worked; I double checked my sources and really don\'t know what I did wrong. It only works

I am getting "CSRF token missing or incorrect".

I already checked Stack Overflow for an answer and nothing worked; I double checked my sources and really don't know what I did wrong. It only works when I comment the MIDDLEWARE_CLASSES line with CsrfViewMiddleware, but I think is something that I never need to do.

Here are the pieces of code I think are relevant:

settings.py

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

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from sitfin.models import Balanta, Conturi, BalantaForm, ConturiForm
from django.forms.formsets import formset_factory

def render_to_response(req,*args,**kwargs):
    kwargs['context_instance']=RequestContext(req)
    return render_to_response(*args,**kwargs)

def conturi_index(request):
    return render_to_response('sitfin/conturi_index.html',{'conturi_list':Conturi.objects.all()})

def conturi_introducere(request):
    ConturiFormSet=formset_factory(ConturiForm)
    if r开发者_开发知识库equest.method=='POST':
        #form=ConturiForm(data=request.POST)
        formset=ConturiFormSet(request.POST, request.FILES)
        #if form.is_valid():
        if formset.is_valid():
            #new_entry=form.save()
            new_entry=formset.save()
            return HttpResponseRedirect("sitfin/conturiok")
    else:
        #form=ConturiForm()
        formset=ConturiFormSet()
    #return render_to_response('sitfin/conturi_introducere.html',{'form':form})
    return render_to_response('sitfin/conturi_introducere.html',{'formset':formset})

The template

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">

    <title>Conturi_introducere</title>

</head>
<body>
    <p>BAGA CONTURILE</p>
    <form action="" method="post">{% csrf_token %}
        {{ formset.management_form }}
        <!--<p><label for="id_cont">cont:</label>{{ form.cont }}</p>
        <p><label for="id_cont_debit">cont debit:</label>{{ form.cont_debit }}</p>
        <p><label for="id_cont_credit">cont credit:</label>{{ form.cont_credit }}</p>
        -->
        <table border="0">
            {% for form in formset %}
            {{ form }}
            {% endfor %}
        </table>

    <p><input type="submit" value="Submit"></p>
    </form>
</body>
</html>

What am i doing wrong?


You need to make sure that you include the RequestContext in your response.

return render_to_response('sitfin/conturi_introducere.html',{'formset':formset},
context_instance=RequestContext(request))


Also I would remove

'django.middleware.csrf.CsrfResponseMiddleware',

This is legacy and is being depreciated for security and performance issues.

Referece

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号