我想使用Django身份验证框架构建身份验证系统。但是我从登录和注销中得到了2个不同的错误,这些错误附在下面。 Django版本:2.1.4
切换到帐户/登录/时:
TypeError at /account/login/
login() missing 1 required positional argument: 'user'
切换到帐户/注销/时:
ValueError at /account/logout/
The view django.contrib.auth.logout didn't return an HttpResponse object. It returned None instead.
某些代码: Views.py
def user_login(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate(username=cd['username'], password=cd['password'])
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse('Authenticated successfully')
else:
return HttpResponse('Disabled account')
else:
return HttpResponse('Invalid login')
else:
return HttpResponse('Form is not valid')
else:
form = LoginForm()
return render(request, 'account/log-in.html', {'form': form})
urls.py
from django.contrib.auth.views import auth_login, auth_logout,
logout_then_login
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/$', auth_login, name='login'),
url(r'^logout/$', auth_logout, name='logout'),
url(r'^logout_then_login/$', logout_then_login, name='logout_then_login'),
url(r'^$', views.dashboard, name='dashboard'),
]
base.html
<div id="header">
<span class="logo">Bookmarks</span>
{% if request.user.is_authenticated %}
<ul class="menu">
<li {% if section == 'dashboard' %}class="selected"{% endif %}>
<a href="{% url 'dashboard' %}">My dashboard</a>
</li>
<li {% if section == 'images' %}class="selected"{% endif %}>
<a href="#">Images</a>
</li>
<li {% if section == 'people' %}class="selected"{% endif %}>
<a href="#">People</a>
</li>
</ul>
{% endif %}
<span class="user">
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }},
<a href="{% url 'logout' %}">Logout</a>
{% else %}
<a href="{% url 'login' %}">Log-in</a>
{% endif %}
</span>
</div>