Django message framework and login_required

前端 未结 5 1075
不知归路
不知归路 2021-02-02 10:34

I\'m using the Django Message Framework to show messages to users as well as the @login_required decorator on one of my views. So if a user tries to access a certai

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 11:27

    I know it's old but it can still help others

    I think to redirect an unconnected user to the login page and at the same time send a message you can proceed as follows:

    from django.contrib import messages
    
    def views(request):
        if request.user.is_anonymous:
            messages.add_message(request, messages.INFO,
                                 'You must be logged in')
            return redirect('your_login_views')
        else:
            # do smothing
            return redirect(request, 'your_page.html')
    

提交回复
热议问题