decorator @login_required does not come back to login page

I am using:

from django.contrib.auth.decorators import login_required

so I have defined a view for user profiles, but obviously, this one must be only accessed whenever its user is logged in, otherwise, the login page should be displayed…

However I obtain the following error message…

NoReverseMatch at /users/profile/

Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/users/profile/
Django Version: 6.0.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.

does someone know to fix this issue…?

You don’t have your auth URLs included in your URL patterns. You need to add something like this: path("accounts/", include("django.contrib.auth.urls")), to your URL config.

You don’t have a URL defined with name="login"

See the docs at Using the Django authentication system | Django documentation | Django for how you need to configure your URLs to use the decorator.

If you think you have configured everything correctly, please post those items here - you may have an error in what you have done.

Also, please watch the profanity. This is intended to be a professional setting and such language does not add to the conversation.

Hi chief

This error doesn’t come from your profile view itself it comes from @login_required trying to build the redirect to your login page.

When an unauthenticated user hits a @login_required view, Django redirects them to settings.LOGIN_URL. If that setting is a URL name (like `‘login’`), Django reverses it to build the redirect. Since there’s no URL pattern named login in your project, the reverse fails and you get NoReverseMatch and it shows up on /users/profile/ because that’s the exact spot where the redirect is being constructed

Touché: I have just set up the LOGIN_URL in file settings.py and it works…thank you very much for your help (all…)

You’re welcome guy

That’s perfect