简体   繁体   中英

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

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 :

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 ????

Note : i can do it in views.py but i don't want!

You can use login_required decorator:

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 . You can customize it by setting LOGIN_URL in your settings.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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