Prevent users who are not logged in from accessing specific pages

Asked 2 years ago, Updated 2 years ago, 96 views

1 Answers

1) Once the user is not logged in, the link to www.mysite.com/codeedit should not be displayed. I think you can handle this part in the views and templates that http://www.mysite.com is connected to.

2) Can the user still type www.mysite.com/codeedit and enter immediately? In that case, in the index,

def index(request):
    if not request.user.is_authenticated():
            return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))

You can do this. Or you can show an error message instead of redirect.

Alternatively,

@login_required
def index(request):
    #Doing something

If this does not automatically log in, settings.Redirect to LOGIN_URL.

Both methods set in advance.You need to set the LOGIN_URL.


2022-09-22 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.