gpt4 book ai didi

python - 如何覆盖Django登录

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:53 24 4
gpt4 key购买 nike

我想覆盖 Django-login 以进行自定义登录,但我找不到方法。

原因是有一种特定情况我不能使用csrf认证,所以我想创建一个自定义登录,然后做一个安全层来确保我的自定义登录是安全的。

有什么想法吗?

最佳答案

要覆盖 django 自定义管理,您必须创建 urls 路径和一个 View ,您可以在其中检查和登录/注销用户。以此为例:

网址.py

url(r'^accounts/auth/$', 'auth_view'),

View .py

from django.contrib import auth

def auth_view(request):

# here you get the post request username and password
username = request.POST.get('username', '')
password = request.POST.get('password', '')

# authentication of the user, to check if it's active or None
user = auth.authenticate(username=username, password=password)

if user is not None:
if user.is_active:
# this is where the user login actually happens, before this the user
# is not logged in.
auth.login(request, user)

...
return ...

else :
return HttpResponseRedirect("Invalid username or password")

您的 html 表单:

<form role="form" action="/accounts/auth/" method="POST">

关于python - 如何覆盖Django登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23567373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com