简体   繁体   English

如何正确配置认证视图? (姜戈)

[英]How to properly configure authentication views? (Django)

There are two views index and index1 and my login view is有两个视图 index 和 index1 ,我的登录视图是

def loginview(request):
    if request.user.is_authenticated:
        return redirect('index')
    else:
        return redirect('index2')

urls.py网址.py

urlpatterns = [
       path('',loginview,name="login"),
       path('index/',index,name="index"),
       path('index2/',index2,name="index2"),
       ]

The code works but I want to only access index and index2 after the user is logged on or logged out.该代码有效,但我只想在用户登录或注销后访问 index 和 index2 。

When I navigate to localhost:8000/index and localhost:8000/index2, the page directs to the respective pages.当我导航到 localhost:8000/index 和 localhost:8000/index2 时,页面会指向相应的页面。

How to restrict authorization on these pages?如何限制对这些页面的授权?

You can add a flag on your django base user class definition to create a state to control this problem您可以在 django 基本用户 class 定义上添加一个标志来创建 state 来控制此问题

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):

    # other User definitions
    index_page = models.CharField(max_length = 12)

then when you call loginview you can determine the index page for user for further requests.然后当您调用loginview时,您可以确定用户进一步请求的索引页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM