Pass request.user to view without altering the url

后端 未结 3 749
死守一世寂寞
死守一世寂寞 2021-01-22 17:50

I\'m trying to write a view where the current logged in user\'s information is retrieved.

My view is written as below, and it works perfectly fine as long as I pass the

3条回答
  •  旧时难觅i
    2021-01-22 18:09

    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.

提交回复
热议问题