登录标准VS2019 Django项目模板后出现CSRF验证失败错误
在标准Django项目中,我有一个登录表单
<form action="." method="post" class="form-horizontal">{% csrf_token %}
<h4>Use a local account to log in.</h4>
<hr />
<div class="form-group">
<label for="id_username" class="col-md-2 control-label">User name</label>
<div class="col-md-10">{{ form.username }}
</div>
</div>
div class="form-group">
<label for="id_password" class="col-md-2 control-label">Password</label>
<div class="col-md-10">{{ form.password }}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="hidden" name="next" value="/" />
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
{% if form.errors %}
<p class="validation-summary-errors">Please enter a correct user name and password.</p>
{% endif %}
</form>
urls.py
path('login/',
LoginView.as_view
(
template_name='app/login.html',
authentication_form=forms.BootstrapAuthenticationForm,
extra_context=
{
'title': 'Log in',
'year' : datetime.now().year,
}
),
name='login'),
和forms.py
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext_lazy as _
class BootstrapAuthenticationForm(AuthenticationForm):
"""Authentication form which uses boostrap CSS."""
username = forms.CharField(max_length=254,
widget=forms.TextInput({
'class': 'form-control',
'placeholder': 'User name'}))
password = forms.CharField(label=_("Password"),
widget=forms.PasswordInput({
'class': 'form-control',
'placeholder':'Password'}))
我尝试登录后,尽管发送了(403) CSRF verification failed. Request aborted.
的值,但我给了禁止的csrfmiddlewaretoken
。
我还想知道BootstrapAuthenticationForm
中到底嵌入了什么login.html
?我没有在BootstrapAuthenticationForm
中提及login.html
。