Django-索引上的自定义登录页面

问题描述 投票:0回答:1

我正在使用Django,并且正在创建一个后端Web应用程序。我想问其他的Django用户,是否可以为登录页面创建URL和函数,但要在索引上。

[未使用默认的Django auth框架进行身份验证/登录。我将为此编写函数,但是我不知道它是否会起作用。我也看过其他教程,但我确实倾向于发现它们已经过时。

我想要的是,一旦加载了应用程序,就会向用户提示一个登录框,一旦他们登录了自己的详细信息,便将他们重定向到自定义仪表板区域(不使用内置的django)。

我希望这会使它更容易理解。

任何信息将不胜感激。

**#Urls.py File**
path('', auth_views.LoginView.as_view(template_name='index.html')),

Views.py文件

@login_required
def index(request, user):
user = authenticate(username=username, password=password)
if user is not None:
    if user.is_active:
        login(request, user, 'index.html')

return HttpResponseRedirect ('dashboard.html')
python django django-admin django-urls django-login
1个回答
0
投票

简而言之,我想知道是否有可能覆盖自定义Django的“帐户/登录”部分。这是可能的,但是需要进行大量调整,并确保一切正确。

我只能希望这对以后的人有所帮助。

Urls.py

path('', LoginView.as_view(template_name='index.html'), name="login"),

Views.py

@login_required
def index(request):
return render(request, 'index.html')   

def login(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
    # Correct password, and the user is marked "active"
    auth.login(request, user)
    # Redirect to a success page.
return render(request, 'admin/dashboard.html')  

def dashboard(request):
return render(request, 'admin/dashboard.html') 

Settings.py

LOGIN_REDIRECT_URL = '/admin/dashboard'

我还有更多功能要实现,但希望对其他人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.