簡體   English   中英

Django password_reset,處理使用的URL

[英]Django password_reset, handling used URL

我正在使用的應用程序將使用戶成功重置密碼; urls.py包含以下內容:

url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
                       'django.contrib.auth.views.password_reset_confirm',
                       name='password_reset_confirm')

這可以按預期工作,將用戶定向到以下URL:

http://example.com/reset/MTNzgN/3ly-466604bc1524f789c120/

..可以重設密碼的地方。

到目前為止,這不是問題,但是如果用戶再次訪問該鏈接(似乎有些用戶已經在執行此操作),則他們會收到相同的更改密碼的邀請,只是在這種情況下不會出現當它們在框中輸入值時具有任何作用。 這導致一些混亂。

這是Django的預期行為嗎? 如果沒有,是否有任何方法可以覆蓋django.contrib.auth.views.password_reset_confirm並在那里檢測使用的令牌,或者重定向到失敗警告? 理想情況下,最好向他們顯示“此令牌已被使用,請嘗試再次重置您的密碼”屏幕,而不是重置后顯示失敗消息,但即使是后者,也比現在做的更好。

我嘗試使用基於類的視圖這樣:

查看

class PasswordResetConfirmView(FormView):
    template_name = "test_template.html"
    success_url = '/account/login'
    form_class = SetPasswordForm

    def get(self, request, uidb64=None, token=None, *arg, **kwargs):
        UserModel = get_user_model()
        try:
            uid = urlsafe_base64_decode(uidb64)
            user = UserModel._default_manager.get(pk=uid)
        except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
            user = None
        if user is not None and default_token_generator.check_token(user, token):
            return super().get(request, uidb64=None, token=None, *arg, **kwargs)

        else:
             messages.error(request,'The reset password link is no longer valid.')
            return redirect('/') 

網址

urlpatterns += patterns('',
                       url(r'^account/reset_password_confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', PasswordResetConfirmView.as_view(),name='reset_password_confirm'),
                       )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM