我正在尝试编写一个注销视图,该视图返回我的应用程序的主页。 结果是正确调用了主页,但用户未注销。为什么注销无法正常工作?
views.py
class BaseView(View):
left_menu_bar = get_categories() # not importatn here
context = {'left_menu_bar': left_menu_bar, 'username': ''}
template_name = ''
extra_context = {}
for element in extra_context:
context[element] = extra_context[element]
def get(self, request):
if request.user.is_authenticated:
username = request.user.username
self.context['username'] = username
return render(request, 'buybook/' + self.template_name, self.context)
class Main(BaseView):
template_name = 'main.html'
class LogOut(View):
def get(self, request):
logout(request)
return HttpResponseRedirect(reverse('buybook:main'))
urls.py
app_name = 'buybook'
urlpatterns = [
#...
path('', views.Main.as_view(), name='main'),
path('logout', views.LogOut.as_view(), name='logout'),
]
和模板:
base.html
<html>
{%if username%}
Your nickname is <b>{{username}}</b>
<a href='{%url 'buybook:logout' %}'> log me out</a>
<br><br>
{%else%}
You're not logged in<br>
<a href='{%url 'buybook:loginpanel' %}'>login</a><br>
<a href='{%url 'buybook:registrationpanel' %}'>create an account</a>
{%endif%}
{%block books %}
{%endblock%}
</html>
main.html
<html>
{% extends 'buybook/base.html' %}
{% block books %}
<h1>Welcom</h1>
<p>
etc.
</p>
{% endblock %}
</html>
我之前已经创建了注册和登录视图,它们可以正常工作,因此我不会粘贴这些视图的代码。仅注销视图是一个问题。