How to reset user password from admin interface

On my website, I want to tell administrators to reset the password of any user.

With reset, I mean exactly what the password_reset view does (in contrib.auth): Send a confirmation link to this user email.

What would be the best way to do this? Is there already an application / snippet that does this?

Edit:

Suppose user john is an administrator. I want john to reset any user password through the admin interface. For example, before the password reset max, he simply goes to the maximum user and clicks on any link to reset his password.

+6
source share
3 answers

What I did in the end was to add a custom ModelAdmin :

 from django.contrib.auth.forms import PasswordResetForm from django.contrib.auth.admin import UserAdmin class CustomUserAdmin(UserAdmin): ... def reset_password(self, request, user_id): if not self.has_change_permission(request): raise PermissionDenied user = get_object_or_404(self.model, pk=user_id) form = PasswordResetForm(data={'email': user.email}) form.is_valid() form.save(email_template_name='my_template.html') return HttpResponseRedirect('..') def get_urls(self): urls = super(UserAdmin, self).get_urls() my_urls = patterns('', (r'^(\d+)/reset-password/$', self.admin_site.admin_view(self.reset_password) ), ) return my_urls + urls 

and I also had to override the change_form.html template, for example:

 {% extends "admin/change_form.html" %} {% load i18n %} {% block object-tools %} {% if change %}{% if not is_popup %} <ul class="object-tools"> {# You can also give a name to that pattern and refer to it below using 'url' #} <li><a href="reset-password/" class="historylink">Reset password</a></li> <li><a href="history/" class="historylink">{% trans "History" %}</a></li> {% if has_absolute_url %} <li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink"> {% trans "View on site" %}</a> </li> {% endif%} </ul> {% endif %}{% endif %} {% endblock %} 

The result is as follows:

Reset Password from admin in Django

If you want a more detailed explanation, I talked about this .

+7
source

The passreset app simply exposes django views via urls.py and customizes the login template to show the Forgot Password link.

Built-in django password reset views and templates are designed for self-reset. I assume that the reset form may be pre-populated with a different user email address (in the query line), but you still need to make adjustments, such as changing the email template: "You receive this email because you requested a reset password for your user account "is probably not what you want:

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/templates/registration/password_reset_email.html

Therefore, you must display the views in different URLs if you want to enable self-reset. Take a django view in urls.py, for example:

 urlpatterns += patterns('django.contrib.auth.views', url(r'^accounts/password/reset/$', 'password_reset', name='password-reset'), url(r'^accounts/password/reset/done/$', 'password_reset_done', name='password-reset-done'), url(r'^accounts/password/reset/confirm/(?P<uidb36>[-\w]+)/(?P<token>[-\w]+)/$', 'password_reset_confirm', name='password-reset-confirm'), url(r'^accounts/password/reset/complete/$', 'views.password_reset_complete', name='password-reset-complete') ) 

and where you want to make adjustments, go for example. your own email template:

 url(r'^/accounts/password/reset/$', 'password_reset', {'email_template_name': 'my_templates/password_reset_email.html'} name='password-reset'), 

There are more options in the password_reset view that you can configure: https://docs.djangoproject.com/en/dev/topics/auth/#module-django.contrib.auth.views (post_reset_redirect comes to mind as one more for your purposes)

To show the corresponding link, you either change the User Administrator (careful, already registered - unregister, then register your own, subclassified plus additional link field) or the change_form template itself.

I do not know about the application that provides this ready-made version, so I supported the question :-).

+2
source

Yes, there is an application for this. Check here:

https://github.com/bendavis78/django-passreset

0
source

Source: https://habr.com/ru/post/914002/


All Articles