Pass request.user to view without altering the url

老子叫甜甜 提交于 2019-12-02 01:36:07

You don't really have to pass anything via url. You can directly access the logged in user from request.user. You can read about this over here. Also, you can check if the user is logged in using is_authentcated() method or using the login_required decorator

def ur_view(request):
   #Check if the user is logged in
   if not request.user.is_authenticated():
          #your logic if user not logged in
   else:
        #If the user is logged in

You need the login_required decorator.

@login_required
def index(request, template="index.html"):
    user = request.user # get the currently logged in user

If the user is logged in he/she can access the activity feed otherwise they will be redirected to login page. This will make sure that only authenticated user can access this view. And you can always access the current user using request.user.

The logged in user object is in the request object too. use: request.user

see more info in the django auth docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!