【问题标题】:Logout function in Django not working as ExpectedDjango 中的注销功能无法按预期工作
【发布时间】:2021-07-17 07:23:14
【问题描述】:

我没有使用任何中间件,当我点击主页模板上的注销按钮时,注销功能执行没有任何错误。但是当我返回主页而不跳转到登录页面时..我看到了我自己是登录用户

这是我的 authentiCation/views.py

from django.shortcuts import render
from django.http import request,HttpResponseRedirect
# for user creation & login form
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login
# for user related Queries
from django.contrib.auth.models import User


# imports for test purpose
from django.http import HttpResponse

# Create your views here.

# register page 
def register_Page(request):
    if request.method == 'POST':
        form= UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username= request.POST['username']
            password= request.POST['password1']
            user= authenticate(request,username=username,password=password)
            login(request,user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse('Either the user name is not available or you may have filled the form incorrectly')
    else:
        form = UserCreationForm()
        context= {'form':form}
        return render(request,'authentication/register_Page.html',context)

# login page
def login_page(request):
    if request.method == 'POST':
        username= request.POST['username']
        password= request.POST['password']
        # returns user if credentials are valid
        user= authenticate(request, username=username, password= password)
        # check if user var contains the user
        if user is not None:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse('Invalid credentials')

    return render(request,'authentication/login.html')

# logout Page
def log_out(request):
    logout(request)
    return HttpResponseRedirect('logout_page')

认证/urls.py

from django.urls import path
from authentiCation import views

urlpatterns = [
    path('register/',views.register_Page),
    path('login/',views.login_page,name='login_page'),
    path('logout/',views.log_out),
]

主要应用文件 网址

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('App_wfi_Community.urls')),
    path('Ask/',include('askQuestion.urls')),
    path('auth/',include('authentiCation.urls')),
]

Home.html

{% extends "basic.html" %}
{% load humanize %}
{% block title %}WFI-Community{% endblock title %}
{% block body %}

<!--  search button   -->
<h5>Welcome {{username}},<h5> <!--  I see my username here   -->
<form action="/search" method="get">
<div class="container py-3 row">
  <div class="col-md-8 offset-2">
    <div class="input-group">
      <input name="searchfieldText" type="text" class="form-control" placeholder="Search">
      <button class="btn btn-danger" type="submit">Search</button>
      <span><a href="{% url 'login_page' %}">Logout</a></span>
    </div>
  </div>
</div>
</form>
<!--  and some more HTML stuff which is irrelavent here -->

【问题讨论】:

  • 怎么知道点击Logout链接执行了注销功能?链接是 login_page。
  • @BradMartsberger 实际上我忘记了.. 现在我已经纠正了这个

标签: django django-views django-forms django-authentication django-auth-models


【解决方案1】:

为此命名,例如 path('logout/',views.log_out, name="logout" ) 并在 home.html 中更改它。 例如 "{% url '注销' %}">注销

【讨论】:

  • 效果很好,但现在的问题是网址不断像这样http://127.0.0.1:8000/logout/login_page
  • 但我想要这样的网址 => 用于注销页面= http://127.0.0.1:8000/logout 用于登录页面= http://127.0.0.1:8000/login_page
  • 这是唯一的方法。必须是这样。
  • 如果它解决了您的问题,请接受作为答案。
  • 但是我为 url 做什么.. 我是初学者,不知道如何处理这个..
猜你喜欢
  • 2020-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
相关资源
最近更新 更多