I'm following this blog for Django password reset. It uses # url('', include('django.contrib.auth.urls', namespace="auth")), for the urls.
Now when the user is logged-in, ie is_authenticated, then these urls should not work.
There's no use of links such as password_change, or password_reset when the user is already logged in. How can I disable those urls when user is authenticated?
Related
I'm using Django-allauth , I can successfully redirect it to home page by adding this in settings.py:
LOGIN_REDIRECT_URL = '/home'
But is there anyway I can redirect it to pervious page?
What you're looking for should actually work out of the box by using the redirect field, which is next by default:
https://docs.djangoproject.com/en/3.1/topics/auth/default/#django.contrib.auth.mixins.AccessMixin.get_redirect_field_name
The view that is checking for the access permission and redirecting to the login form only needs to pass the URL the user was previously on. If that field is present, the user will be redirected to that particular page after logging in.
How are you restricting login? I assume you're not passing next? The #login_required decorator and the LoginRequiredMixin for class-based views both set next by default.
This is what the docs say about how LoginView handles POST requests:
If called via POST with user submitted credentials, it tries to log the user in. If login is successful, the view redirects to the URL specified in next. If next isn’t provided, it redirects to settings.LOGIN_REDIRECT_URL (which defaults to /accounts/profile/).
I don't feel that the original django admin login is secure, so I want to have /admin always redirect to my AllAuth login page, even if the user is logged in.
urls.py
admin.site.login = login_required(admin.site.login)
This will redirect users from the django admin login page if they are not logged in, but it does not redirect users if they ARE logged in. So they can still brute force it. How do I edit the login_required decorator to check for is_superuser.
You can use:
from django.contrib.auth.decorators import user_passes_test
admin.site.login = user_passes_test(lambda u: u.is_superuser)(admin.site.login)
If the user is not logged-in, I want it to redirect to the admin page and when the user logs in, he should be redirected to the previous url.
This is how I'm doing:-
LOGIN_URL = '/admin'
LOGIN_REDIRECT_URL = '/admin'
The user does get redirected to admin page but after logging in, the admin dashboard is open, not the next url. That's because the url is not exactly what django is expecting.
This is how the url looks like.
http://127.0.0.1:8000/admin/login/?next=/admin/%3Fnext%3D/movies/fav%253Fpage%253D1
What am I missing?
Tried this. And it worked.
LOGIN_URL = '/admin/login/'
allauth django
I would like to redirect a user to a "dynamic" login page.
I am currently being redirected to accounts/login
but I would like it to be
accounts/login/?name=Bob for user Bob
accounts/login/?name=Carl for user Carl
accounts/login/?name=Alice for user Alice
etc ...
Note that I could change the LOGIN_URL in settings.py to be e.g. accounts/login/?name=Bob but that would remain fixed and I would not get accounts/login/?name=Carl or accounts/login/?name=Alice.
Once the user has successfully login, I would need to retrieve the name from the request e.g. If user Bob has successfully login, get the name=Bob, etc.
Usually, I would do request.GET.get('name','')
but I would need to change allauth views
Is there a way to achieve the above without changing the source code of allauth.
I have this URL which takes user to the login page:
url(r'^login_backend', 'fileupload.backend.login_backend'),
Since the user doesn't want to remember:
http://127.0.0.1:8000/login_backend
I want to redirect http://127.0.0.1:8000 to http://127.0.0.1:8000/login_backend without affecting any other activity. I have this decorator above every view:
#login_required(login_url='/login_backend/')
You can use django generic redirect_to view. Add following in your urls.py
urlpatterns = patterns('django.views.generic.simple',
('^$', 'redirect_to', {'url': 'login_backend/'}),
)