Principiante in Django qui, ho cercato di sistemarlo da molto tempo. Ho 'django.middleware.csrf.CsrfViewMiddleware' nelle mie classi middleware e ho il token nel mio modulo di post.Token CSRF mancante o errato
Ecco il mio codice, cosa sto sbagliando?
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from chartsey.authentication.forms import RegistrationForm
from django.template import RequestContext
from django.core.context_processors import csrf
def register(request):
if request.method == 'POST':
c = RequestContext(request.POST, {})
form = RegistrationForm(c)
if form.is_valid():
new_user = form.save()
return HttpResponseRedirect("/")
else:
form = RegistrationForm()
return render_to_response("register.html", {'form': form, },)
Ecco il mio modello:
{% block content %}
<h1>Register</h1>
<form action="" method="POST"> {% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
possibile duplicato di [Django: CSRF Token mancante o errato] (http://stackoverflow.com/questions/8321217/ django-csrf-token-missing-or-incorrect) – lracicot