I am trying to add a user reg system to my Django site. Obviously, I want to use the built-in auth views and forms. I am going about implementing the auth password reset process. It works fine up to sending the email, but then does not redirect properly. The end result is the password reset done email being sent over and over and over. I am overriding the templates, but nothing else, right now all they have is:
{{form.As_p}}
I have changed my urls.py to use the auth.views as such:
from django.contrib.auth import views as auth_views
(r'^account/forgot/$',
auth_views.password_reset,
{'template_name': 'registration/password_reset.html',
'post_reset_redirect':'/account/password-reset-done'}
),
(r'^account/password-reset-done/$',
auth_views.password_reset_done,
{'template_name': 'registration/password_reset_done.html'}
),
It looks to me as though you need a trailing slash on the post_reset_redirect url. Have you tried that? At the moment, /account/password-reset-done won't match r'^account/password-reset-done/$' because the / in that is compulsory.
See example request 4 in the Django url dispatcher documentation: https://docs.djangoproject.com/en/dev/topics/http/urls/#example
Related
Django allauth fails when using a custom user model with the email as the primary key, I learnt about this when I tried to test the password change functionality.
Reverse for 'account_reset_password_from_key' with keyword arguments '{'uidb36': 'mgodhrawala402#gmail.com', 'key': 'bbz25w-9c6941d5cb69a49883f15bc8e076f504'}' not found. 1 pattern(s) tried: ['accounts/password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$']
I don't mind going into the code to fix this issue, since my application is still under development, I can still change authentication incase anyone has any suggestions.
The easiest fix for this problem would be adding a sequence integer pk and just updating your email field to index unique because as you see the regex path doesn't accept email format for uidb36, it only accepts 0-9A-Za-z.
If you must use email as pk you can add another path to your urls to accept your email pattern:
from allauth.account import views
path('accounts/', include('allauth.urls')),
re_path(
r"^accounts/password/reset/key/(?P<uidb36>.+)-(?P<key>.+)/$",
views.password_reset_from_key,
name="account_reset_password_from_key_accept_email",
),
On my website users access their accounts by going to the following address:
http://127.0.0.1:8000/users_area/username/profile
There they will find a button 'change password' (an overridden django template) which will take them to:
http://127.0.0.1:8000/accounts/password/change/
It is very small and I'm probably being too picky but is it possible to keep this overridden template but change its url? I'd instead like the change password url to be something like:
http://127.0.0.1:8000/users_area/username/password/change/
I've tried creating a template within my user's app, copying the 'change password' template content into there, then linking to it rather than to the overridden 'change password' template, but (obviously, in hindsight) it doesn't work.
I'll add as a side note. I actually have two user types, users and powerusers. Each has a unique and rather different 'users_area':
http://127.0.0.1:8000/users_area/username/profile
and
http://127.0.0.1:8000/powerusers_area/username/profile
If I get an answer to my above question I'm actually hoping to apply it to both of my different user types (shouldn't be hard but thought I should mention it).
Thank you.
In case you want to customize them, even have a customized MyUser model, you can still re-use a lot from Django like this
# authentication
path('user_register', user_register_view, name='user_register'),
path('login', login_view, name='login'),
path('logout', logout_view, name='logout'),
path('change_password', change_password_view, name='change_password'),
path('reset_password/',
auth_views.PasswordResetView.as_view(template_name="MyUser/password_reset.html"),
name="reset_password"),
path('reset_password_sent/',
auth_views.PasswordResetDoneView.as_view(template_name="MyUser/password_reset_sent.html"),
name="password_reset_done"),
path('reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name="MyUser/password_reset_form.html"),
name="password_reset_confirm"),
path('reset_password_complete/',
auth_views.PasswordResetCompleteView.as_view(template_name="MyUser/password_reset_done.html"),
name="password_reset_complete"),
path('profile/<username>', profile_view, name='profile'),
You can do something like this
urls.py
from django.contrib.auth import views as auth_views
urlpatterns = i18n_patterns(
# other URLS
path('/users_area/username/password/change/', auth_views.PasswordChangeView.as_view, name='change_password'),
# more URLS from auth?
)
I am trying to redirect django index url to admin url which I can do something like below:
# url.py
path("", admin.site.urls,),
url("^admin/", admin.site.urls, name="admin"),
However, this generating warning
WARNINGS:
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
Hence, I decided to create a index view and use django.view.genertic.redirectview to pass to Django admin root URL as url attribute. I am trying find better way to generate Django admin root url using reverse function.
Ahh, found it. might help to someone.
reverse('admin:index', current_app=self.name)
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/'}),
)
I am making a custom administration page in Django. I do not want to reinvent the wheel and thus want to use Django admin login form for the staff to log in and redirect them to /my-url/ afterwards.
However, I can't find the way to redirect user to a custom url after successful login at /admin/.
since I stumbled across the same problem, I noticed the url of the the default login page:
/admin/login/?next=/admin/
so I changed the login page link to
/admin/login/?next=/
to point to the main page
works for the logout page too, nice and simple
I had the same issue.
Instead of redirect after login I used the #staff_member_required decorator
for my view /my-url/ which redirects to the admin login
from django.contrib.admin.views.decorators import staff_member_required
#staff_member_required
def test_list(request):
return HttpResponse('TEST')
If class based views is used check out the method_decorator
The Django auth app comes with a login view which you can hook up to /accounts/login/ or any other url you choose. You can probably use the admin's login template admin/login.html if you don't want to write your own.
By using the login view, the LOGIN_REDIRECT_URL parameter will work. The purpose of the /admin/ page is to display the admin index. I would avoid trying to use it as the login page.
Set LOGIN_REDIRECT_URL in your settings.py file. Documented here.
If you have ?next= parameter set is not enough to set LOGIN_REDIRECT_URL in you setting.py. You need to also handel this parameter. As say docs (https://docs.djangoproject.com/en/4.1/ref/settings/#login-redirect-url):
LOGIN_REDIRECT_URL
Default: '/accounts/profile/'
The URL or named URL pattern where requests are redirected after login
when the LoginView doesn’t get a next GET parameter.
In my case I just set REDIRECT_FIELD_NAME = "" instead of REDIRECT_FIELD_NAME = "next" in my django.contrib.auth and it works fine.