【问题标题】:Problems with Django logout on URLsDjango 在 URL 上注销的问题
【发布时间】:2018-11-13 09:34:38
【问题描述】:

我是 Django 的初学者,我已经解决了我的问题,但我想了解...

我的应用上有一个登录页面和一个注销页面,如下:

urls.py:

url('deconnexion', views.logout, name='deconnexion'),
url('connexion', views.connexion, name='connexion'),

views.py:

def connexion(request):
    error = False

    if request.method == "POST":
        form = ConnexionForm(request.POST)

        if form.is_valid():

            username = form.cleaned_data["username"]
            password = form.cleaned_data["password"]
            user = authenticate(username=username, password=password)  

            if user: 
                login(request, user)  
            else: 
                error = True
    else:
        form = ConnexionForm()

    return render(request, 'dashboard/connexion.html', locals())


@login_required(login_url='/dashboard/connexion/')
def logout(request):
   django_logout(request)
   return redirect(reverse(connexion))

如果我更改 url: connexion 代替 deconnexion 的位置,我的脚本将不起作用...我没有注销我,我被重定向到正在连接的 connexion 页面...

如果有人有想法?

P.S.:对不起,我的英语很差,我是法国人......而且法语和英语......我们都知道这很复杂......对不起;)

【问题讨论】:

  • 你可以使用Django User Authentication,那么你就不用自己处理这些认证操作了。
  • 请澄清:“换地方”是什么意思?在 urls.py 或 views.py 中?何和 bienvenue 到 StackOverflow :)
  • 我不太明白你做了什么,但问题可能出在你的网址上;您应该在模式之前和之后使用锚点 (r"^connexion$") 或使用 path() 而不是 url()
  • 非常感谢您的帮助和欢迎信息 :) 确实,它在我的网址中,谢谢@Régis B. MD。 Khairul Basar 和 Daniel Roseman 的帮助,这是我的主播...我是菜鸟 XD
  • 问题可能是由于r"connexion" 正则表达式匹配“deconnexion”字符串。

标签: django logout


【解决方案1】:

如 django documentations 中所述,您可以这样做:

from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    return redirect(reverse(connexion))

【讨论】:

    猜你喜欢
    • 2018-05-22
    • 2011-01-05
    • 2018-11-29
    • 2023-03-29
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    相关资源
    最近更新 更多