Opened 5 years ago
Closed 5 years ago
#31288 closed New feature (wontfix)
New ReplaceAuthMiddleware feature.
Reported by: | Théo Huchard | Owned by: | Théo Huchard |
---|---|---|---|
Component: | contrib.auth | Version: | dev |
Severity: | Normal | Keywords: | django contrib auth middleware ReplaceAuthMiddleware 3.0 ease of use for big projects |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hello,
I have developped a new feature as a middleware that I am using for a personal project. I think it could be a great implementation for Django.
Here is an example of usage in the settings.py
file:
Code highlighting:
REPLACE_URL_IF_UNLOGGED = { "replace": ["index", "logout"], "by": "/login" } REPLACE_URL_IF_LOGGED = { "replace": ["login"], "by": "/" }
The goal here is to redirect the user if he is logged or unlogged directly from the replace URL.
In my example, if the user is unlogged and trying to access the URL that was defined in urls.py
like below:
Code highlighting:
urlpatterns = [ path('', index, name="index"), path('login', login.view, name='login'), path('logout', logout.execute, name='logout') ]
...he will automatically be redirected to the "by"
element, just by checking if the user is logged or not.
So in this example, if the user is actually logged in and tries to access the login
URL defined in urls.py
he'll be redirected to the index
URL.
And if the user is unlogged and try to access the index
or logout
URL, he'll be redirected to the login
URL.
I thing that it might be a nice addition to the example in the doc:
Code highlighting:
from django.contrib.auth.decorators import login_required @login_required def my_view(request): ...
...since it can ease URL redirection directly from the settings.py
file instead of using decorator.
Attachments (1)
Change History (4)
by , 5 years ago
Attachment: | middleware.py added |
---|
comment:1 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 5 years ago
Version: | 3.0 → master |
---|
comment:3 by , 5 years ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
Summary: | New ReplaceAuthMiddleware feature → New ReplaceAuthMiddleware feature. |
Thanks for this ticket, however I don't think that keeping in settings the list of URLs which require to be logged is something that we would like to add to Django. We've already provided multiple ways to achieve this.
The middleware code