下午好,
我是django的新手,我正在创建一个登录门户,但是我收到以下错误| CSRF验证失败。请求中止。
我正在使用Django 1.8和Python 3.4请帮助我,如果您还有其他需要请告诉我
这是我的views.py:
getActiveView()
Este es mi archivo ingresar.html
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
def nuevo_usuario(request):
if request.method == 'POST':
formulario = UserCreationForm(request.POST)
if formulario.is_valid():
formulario.save()
return HttpResponseRedirect('/')
else:
formulario = UserCreationForm()
return render_to_response('registration/nuevo_usuario.html', {'formulario':formulario}, context_instance = RequestContext(request))
def ingresar(request):
if request.method == 'POST':
formulario = AuthenticationForm(request.POST)
if formulario.is_valid():
usuario = request.POST['username']
clave = request.POST['password']
acceso = authenticate(username=usuario, password=clave)
if acceso is not None:
if acceso.is_active:
login(request, acceso)
return HttpResponseRedirect('/privado')
else:
return render_to_response('registration/noactivo.html', context_instance = RequestContext(request))
else:
return render_to_response('registration/nousuario.html', context_instance = RequestContext(request))
else:
formulario = AuthenticationForm()
return render_to_response('registration/ingresar.html', {'formulario':formulario}, context_instance = RequestContext(request))
Este es mi archivo url.py
{% extends 'base.html' %}
{% block titulo %}Ingresa al sistema{% endblock %}
{% block encabezado %}
Ingresa
{% endblock %}
{% block content %}
<div class="main">
<div class="login-form">
<h1> C. Monitoring Center</h1>
<div class="head">
</div>
<form id = 'formulario' method = 'post' action = ''> {% csrf_token %}
<input type="text" class="text" placeholder="Username" required autofocus>
<input type="password" placeholder="Password" required>
<div class="submit">
<input type="submit" onclick="#" value="LOGIN" >
</div>
<p><a href="#">Forgot Password ?</a></p>
</form>
</div>
{% endblock %}
答案 0 :(得分:0)
你需要一个django提供的csrf令牌......(necesitas la clave csrf q django tiene incluido)
<form id = 'formulario' method = 'post' action = ''> {% csrf_token %}
<input type="text" class="text" placeholder="Username" required autofocus>
<input type="password" placeholder="Password" required>
<div class="submit">
<input type="submit" onclick="#" value="LOGIN" >
</div>
{% csrf_token %} <!-- THIS!!!!! Aqui !!!-->
<p><a href="#">Forgot Password ?</a></p>
</form>
或者如果你想要你可以禁用csrf保护(o si quierres se puede apagar los protejos de csrf)
@csrf_exempt # you need to import from django.contrib.auth
def ingresar(request):