简体   繁体   English

如果 urls.py 中的条件 - django

[英]if condition in urls.py - django

I want to check if user not loged (don't have a session) he go to login.html file and if he loged (have a session) he go to profile.html , but i want to check it at urls.py我想检查用户是否没有登录(没有会话)他去 login.html文件,如果他登录(有一个会话)他去profile.html ,但我想在 urls.py 上检查

My projects Tree :我的项目树:

manage.py
url
----setting.py
mr_url
-----templates
---------url
------------profile.html
------------login
-----------------logon.html
-----views.py
-----models.py
-----urls.py

My urls.py :我的urls.py

from django.conf.urls import url
from . import views
from django.views.generic import TemplateView

urlpatterns =[
    url(r'^$', views.profile, name='profile'),
]

How i can check set session for user or not in urls.py ????我如何在urls.py 中检查用户的设置会话????

Note : i can do it in views.py but i don't want!注意:我可以在views.py但我不想要!

You can use login_required decorator:您可以使用login_required装饰器:

from django.contrib.auth.decorators import login_required

urlpatterns =[
    url(r'^$', login_required(views.profile), name='profile'),
]

If the user is not logged in, he will be redirected to the login page whose default is accounts/login .如果用户没有登录,他将被重定向到默认为accounts/login的登录页面。 You can customize it by setting LOGIN_URL in your settings.您可以通过在设置中设置LOGIN_URL来自定义它。

Hope it helps!希望能帮助到你!

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

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