在这里是Django的初学者,我已经尝试了很长时间了。我的中间件类中确实有“ django.middleware.csrf.CsrfViewMiddleware”,并且我的帖子中确实有令牌。
这是我的代码,我在做什么错?
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, }, )
这是我的模板:
{% block content %} <h1>Register</h1> <form action="" method="POST"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit"> </form> {% endblock %}
你应该使用自动使用的render快捷功能return render(request, 'template.html'),RequestContext
render
return render(request, 'template.html'),RequestContext
{% csrf_token %}