我要做的是:
我试图在用户点击退出按钮时注销用户。
我遇到了什么问题:
点击退出按钮时出现以下问题:
我的代码: -
article.html:
<div class="container">
<nav class="nav blog-nav">
<a class="nav-link" href="#">Library</a>
{% if user.is_authenticated %}
<a style="float: right;" class="nav-link" href="{% url 'librarysystem:Logout' %}">Logout</a>
<a style="float: right;" class="nav-link" href="#">{{ request.user.username }}</a>
{% if user.UserProfile.avatar == None %}
<a style="float: right;" class="nav-link" href="#"><img class="img-thumbnail" src="/static/defaultPic/defaultPic.png" class="img-responsive" alt = "Generic placeholder thumbnail"/></a>
{% else %}
<a style="float: right;" class="nav-link" href="#" class="thumbnail"><img class="thumbnail" src="{{ request.user.UserProfile.avatar }}" class="img-responsive"/></a>
{% endif %}
{% endif %}
</nav>
</div>
登录查看:
def loginUser(request):
data = {}
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')
data['responseMessage'] = ''
user = authenticate(username=username,password=password);
if user is None:
data['response'] = False
else:
if user.is_active:
data['responseMessage'] = 'Already logged in.'
else:
login(request,user)
data['redirectTo'] = "/librarysystem/article/"
data['response'] = True
return JsonResponse(data)
退出查看:
def logoutUser(request):
logout(request)
template = 'librarysystem/Elib.html'
return render(request, template)
urls.py:
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^register/$',views.registerUser),
url(r'^$', views.index, name="Index"),
url(r'^validateRegisterForm/$',views.validateRegisterForm),
url(r'^validateLoginForm/$',views.validateLoginForm),
url(r'^article/$', views.article, name="Article"),
url(r'^Login/$',views.loginUser, name="Login"),
url(r'^Logout/$',views.logoutUser, name="Logout"),
]
答案 0 :(得分:4)
如错误日志所示:
global name 'logout' is not defined
您需要先从django.contrib.auth导入注销。 使用此:
from django.contrib.auth import logout
像这样:
from django.contrib.auth import logout
def logoutUser(request):
logout(request)
template = 'librarysystem/Elib.html'
return render(request, template)
# Redirect to a success page.