由于某种原因,当一个人登录失败时,代码没有显示验证错误(显示在 forms.py 中(。它只显示空模板。我相信模板被覆盖了,但我不确定我的代码中的哪位是问题所在:
views.py
def log_in(request):
form = LogInForm(request.POST or None)
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('menu')
return HttpResponseRedirect("/login")
else:
return render(request, 'login.html', {'form': form})
登录.html
{% extends 'html/base.html' %}
{% load bootstrap %}
{% load static %}
{% block add_head %}
<link rel="stylesheet" href="{% static 'css/login.css' %}">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<link rel="stylesheet" href="{% static 'css/menu.css' %}">
{% endblock %}
{% block logout %}
<a href="{% url 'index' %}" class="button" type="button"
style="vertical-align:middle; background-color: red; width: 7%;"><span>Home</span></a>
{% endblock %}
{% block content %}
<div class="container">
<div class="row" style="margin-top:20px; text-align: center; font-size: large">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<form role="form" method="POST" enctype="multipart/form-data">
<hr class="colorgraph" style="margin-top: 0">
{{ form|bootstrap }}
<hr class="colorgraph">
{% csrf_token %}
<div style="text-align: center;">
<input type="submit" class="btn btn-lg btn-primary inline-block"
style="width: 30%; background: purple; border: purple;" value="Log in"/>
<a href="{% url 'signup' %}" class="btn btn-lg btn-primary inline-block"
style="width: 30%; background: purple; border: purple;" type="button"><span>Sign-up</span></a>
</div>
</form>
</div>
</div>
</div>
<link rel="script" href="{% static 'js/login.js' %}">
{% endblock %}
{% block user_message %}
{% endblock %}
forms.py
class LogInForm(forms.ModelForm):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'password')
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
user = authenticate(username, password)
if not user or not user.is_active:
raise forms.ValidationError("Sorry, that login was invalid. Please try again.")
return self.cleaned_data
urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^signup/$', views.sign_up, name='signup'),
url(r'^login/$', views.log_in, name='login'),
url(r'^activate/$', views.activate_user_account, name='activate_user_account'),
]
非常感谢。 这似乎是一件非常小的事情,因为它可以正常工作,只是不显示错误。
你必须打电话给form.is_valid()
def log_in(request):
form = LogInForm(request.POST or None)
if request.method == 'POST':
if not form.is_valid(): # Here
return render(request, 'login.html', {'form': form})
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('menu')
return HttpResponseRedirect("/login")
else:
return render(request, 'login.html', {'form': form})