问题
I am trying to use django-password-reset (https://pypi.python.org/pypi/django-password-reset/0.2) to allow users recovering their passwords.
django-password-reset provides a form where the user can enter their username or email address, and tries to find one of those in the database. In my project, the user has no username field - only the email field. Thus, I need to make it search users by the 'email' field only, and not by the 'username' field, which does not exist in my system.
The projects documentation says there are attributes to change this behavior: http://django-password-reset.readthedocs.org/en/latest/views.html.
However, I simply cannot find out how to set these attributes.
Can someone help?
回答1:
Just override Recover
view like so:
class MyRecover(Recover):
search_fields = ['email']
myrecover = MyRecover.as_view()
because there are two recover urls, you need to pass the one with signature first and then the overridden one and then the included one, seems not very DRY, but it's the only way to make the urls be dispatched correctly:
url(r'^recover/(?P<signature>.+)/$', 'password_reset_recover.views.recover_done',
name='password_reset_sent'),
# next we override our url, assuming your app name is called 'core'
url(r'^recover/$', 'core.views.myrecover', name='password_reset_recover'),
url(r'^/', include('password_reset.urls')),
回答2:
The documentation for django-password-reset
says that it uses django.contrib.auth.models.User
(the django built in User
model) to search for username
or email
. So here a username would exist.
If you want to change the functionality of the django-password-reset
you can go into the forms.py
file and change the form's search_fields
variable (and other logic) so that it only searches by email.
Here is the hyperlink to the forms.py:
https://github.com/brutasse/django-password-reset/blob/master/password_reset/forms.py
I'd start with changing the PasswordRecoveryForm
来源:https://stackoverflow.com/questions/21338428/django-password-reset-how-to-set-attributes