Thursday, February 24, 2011

Re: CSRF verification failed. Request aborted.

which version of django are you using ?
1)Check your csrf middleware  in your settings.py
2)add {% except_csrf %}  above your view definition if you don't need it


--Ankit Rai



On Thu, Feb 24, 2011 at 2:24 PM, luca72 <lucaberto@libero.it> wrote:
hello can you hel me to solve the problem:
this is the template:
{% extends "t_base.html" %}
       {% block tittle %}{{titolo}}{% endblock %}
{% block main %}
       <form action="" method="POST" enctype="multipart/form-data">{%
csrf_token %}
   {{form.as_table}}
   <p><input type="submit" value="Salva"></input></p>
   </form>
   {{n_c}}
{% endblock %}

this is the view:

@login_required
def inserisco(request):
   import os, time
   from django.core.files import File
   request.session.set_expiry(0)
   titolo = "Inserisci File"
   if request.user.is_superuser:
       if request.method == 'POST':
           form = Form_inserisco(request.POST, request.FILES)
           if form.is_valid():
               nome_f = form.cleaned_data['nome']
               nome = nome_f.name
               l = nome_f.size
               mio_file = open(nome,'wb')
               for chunk in nome_f.chunks():
                   mf = mio_file.write(chunk)
               mio_file.close()
               lunghezza = (str(l)+' bytes')
               ora_data = time.ctime()
               info = form.cleaned_data['info']
               descrizione = form.clened_data['descrizione']
               salvo_tipi = Finale(nome=nome, dimensione=lunghezza,
data_ora=ora_data, info=info, descrizione=descrizione)
               salvo_tipi.save()
               salv_corr = """<script type="text/javascript">
                           {alert('File salvato correttamente')}
                           </script>"""
               n_c = mark_safe(salv_corr)
               c = {'form':form, 'titolo':titolo, 'n_c':n_c}
               c.update(csrf(request))
               return
render_to_response('inserisco.html',c)
           else :
               form = Form_inserisco()
               salv_corr = """<script type="text/javascript">
                           {alert('Non hai inserito tutti i dati')}
                           </script>"""
               n_c = mark_safe(salv_corr)
               return render_to_response('inserisco.html',
{'form':form, 'titolo':titolo, 'n_c':n_c})
       else:
           form = Form_inserisco()
           return render_to_response('inserisco.html', {'form':form,
'titolo':titolo})

I get CSRF verification failed. Request aborted. after the submit

Thanks Luca

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment