2012-07-16 105 views
0

我在Django中重置密碼時遇到了問題。看着這之後:Resetting Password,這個錯誤仍然存​​在.... 我的錯誤是:在Django中重置密碼

Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{'uidb36': '1', 'token': '392-804fab6dcec790f0ec6b'}' not found. 

這裏是我的urls.py:

urlpatterns = patterns('lex.views', 
    url(r'^home/forgotpassword', 'lexrequestpassword', name='lexrequestpassword'), 
    url(r'^home/resetpassword', 'lexresetpassworddone', name='lexresetpassworddone'), 
    url(r'^home/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'lexresetpasswordconfirmed', name='lexresetpasswordconfirmed'), 
    url(r'^home/resetpasswordcompleted', 'lexresetpasswordcompleted', name='lexresetpasswordcompleted'),) 

我views.py:

@login_required  
def lexrequestpassword(request): 
    """ 
    Reset Password 
    """ 
    path = reverse('lexresetpassworddone') 

    return password_reset(request,post_reset_redirect=path) 

@login_required  
def lexresetpassworddone(request): 
    """ 
    Reset Password Done 
    """ 
    path = reverse('lexresetpasswordconfirmed') 

    return password_reset_done(request,template_name=path) 

@login_required  
def lexresetpasswordconfirmed(request): 
    """ 
    Reset Password Confirmed 
    """ 
    path = reverse('lexresetpasswordcompleted') 

    return password_reset_confirm(request,post_reset_redirect=path) 

@login_required  
def lexresetpasswordcompleted(request): 
    """ 
    Reset Password Completed 
    """ 
    path = reverse('lexmain') 

    return password_reset_complete(request,post_reset_redirect=path) 

不知道如何解決這個問題。需要一些指導...

回答

1

比你的def lexresetpasswordconfirmed(request):還應該接受uidb36和令牌參數。

+0

DEF lexusresetpasswordconfirmed(請求,uidb36,令牌): 「 」 路徑=反向( 'lexusresetpasswordcompleted', 「」「 確認 重置密碼」 kwargs = { 'uidb36': request.uidb36,'token':request.token}) 返回password_reset_confirm(request,post_reset_redirect = path)。像這樣? – lakesh 2012-07-16 10:13:08

1
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{'uidb36': '1', 'token': '392-804fab6dcec790f0ec6b'}' not found. 

這意味着,在執行過程中的某個時刻,你是不是呼籲lexresetpasswordconfirmed逆轉,你調用它django.contrib.auth.views.password_reset_confirm

這個錯誤發生在哪裏?在模板中?如果是這樣,請確保您使用的模板有

{% url lexresetpasswordconfirmed uid token %} 

,而不是

{% url django.contrib.auth.views.password_reset_confirm uid token %} 

是發生在一個視圖中的錯誤?如果是這樣,那麼你在django.contrib.auth.views.password_reset_confirm上撥打反向號碼。

一旦錯誤得到解決,然後是你必須解決另一個錯誤,亞歷山大指出,即包括UUID並在視圖功能標記:

@login_required  
def lexresetpasswordconfirmed(request, uuid36, token): 
    """ 
    Reset Password Confirmed 
    """ 
    # you need to do SOMETHING with the uuid and token here, RIGHT?!?! 
    path = reverse('lexresetpasswordcompleted') 

    return password_reset_confirm(request,post_reset_redirect=path) 

所以我猜每個您正在使用django.contrib.auth中的視圖,這些回報是正確的嗎?問題是,其中一個視圖 - 可能是password_reset_done - 並不在乎你提供了重定向,它使用它自己的。

2

密碼重置 Django具有我們將要使用的內置密碼重置功能。在本教程中,我們將把我們的應用程序命名爲帳戶

urls.py

from django.conf.urls import patterns, include, url 
from django.conf import settings 
from django.conf.urls.static import static 
from django.contrib import admin 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 

    admin.autodiscover() 

    urlpatterns = patterns('', 
     url(
      r'^admin/', 
      include(admin.site.urls) 
     ), 

     #this is the url for password reset authentication 
     #Don't remove, this is important! 
     url(
      r'^account/', 
      include('django.contrib.auth.urls') 
     ), 

     #this is the accounts app main url 
     url(
      r'^accounts/', 
      include('accounts.urls', namespace="accounts") 
     ), 

    )+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

    urlpatterns += staticfiles_urlpatterns() 

urls.py

from django.conf.urls import * 

urlpatterns = patterns('accounts.views', 
    url(
     r'^forgot-password/$', 
     'forgot_password', 
     name="forgot-password" 
    ), 
) 

views.py

from django.contrib.auth.views import password_reset 
from django.shortcuts import render 

def forgot_password(request): 
    if request.method == 'POST': 
     return password_reset(request, 
      from_email=request.POST.get('email')) 
    else: 
     return render(request, 'forgot_password.html') 

把你的base.html文件在主模板文件夾

base.html文件

<html> 
<head> 
    <title>{% block title %}{% endblock title %}</title> 
</head> 
<body> 
    {% block content %}{% endblock content %} 
</body> 
</html> 

把這個模板的應用程序模板文件夾。

forgot_password.html

{% extends "base.html" %} 

{% block title %}Forgot Password{% endblock title %} 

{% block content %} 
<form method="post" action="{% url django.contrib.auth.views.password_reset %}"> 
    {% csrf_token %} 
    <p>Please enter your email address. 
     You will receive a link to create a new password via email.</p> 

    <input type="email" name="email" 
     placeholder="Your e-mail"><br/> 

    <button type="submit">Send new password</button> 
</form> 
{% endblock content %} 

settings.py

#add email settings 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = 'user' 
EMAIL_HOST_PASSWORD = 'password' 
DEFAULT_FROM_EMAIL = 'your email' 

現在我們將覆蓋管理員的密碼重置模板。在你的主模板文件夾中創建註冊文件夾。在註冊文件夾中,創建這些文件:

如果要更改文件的內容。確保它是正確的,否則你會得到一個錯誤。

password_reset_complete.html

{% extends "base.html" %} 

{% block title %}Password reset complete{% endblock title %} 

{% block content %} 
<h4>Reset Complete</h4> 
<p>Your password has been set. 
    You may go ahead and log in now.</p> 
<a href="{% url accounts:login %}">Log in</a> 
{% endblock content %} 

password_reset_confirm.html

{% extends "base.html" %} 

{% block title %}Setting New password{% endblock title %} 

{% block content %} 
<h4>Set New Password</h4> 

<form action="" method="post"> 
    {% csrf_token %} 
    {% if validlink %} 
     <p>Please enter your new password twice. 
      So we can verify you typed it in correctly.</p> 

     <p> 
      {{ form.new_password1.errors }}<br/> 
      {{ form.new_password1 }} 
     </p> 

     <p class="button-height"> 
      {{ form.new_password2.errors }}<br/> 
      {{ form.new_password2 }} 
     </p> 
    {% else %} 
     <h4>Password reset unsuccessful</h4> 
     <p>The password reset link was invalid, 
      possibly because it has already been used. 
      Please request a new password reset.</p><br/> 
    {% endif %} 

    <button type="submit">Change my password</button> 
</form> 
{% endblock content %} 

password_reset_done.html

{% extends "base.html" %} 

{% block title %}Password reset successful{% endblock title %} 

{% block content %} 
<h4>Reset Password</h4> 
<p>We've e-mailed you instructions for setting 
    your password to the e-mail address you submitted.</p> 
<p>You should be receiving it shortly.</p> 
<p><a href="{% url accounts:login %}">Login</a></p> 
{% endblock content %} 


#password_reset_ email.html 
{% load i18n %} 
{% load url from future %} 
{% autoescape off %} 

{% blocktrans %} 
You're receiving this e-mail because you requested 
a password reset for your user account at {{ site_name }}. 
{% endblocktrans %} 

{% trans "Please go to the following page and choose a new password:" %} 

{% block reset_link %} 
{{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb36=uid token=token %} 
{% endblock %} 

{% trans "Your username, in case you've forgotten:" %} {{ user.username }} 

{% trans "Thanks for using our site!" %} 

{% blocktrans %}The {{ site_name }} team{% endblocktrans %} 

{% endautoescape %} 

password_reset_form.html

{% extends "base.html" %} 

{% block title %}Password reset form{% endblock title %} 

{% block content %} 
<h4>Reset Password</h4> 



    <p>Forgotten your password? 
     Enter your e-mail address below, 
     and we'll e-mail instructions for setting a new one.</p> 

    <form action="" method="post"> 
     {% csrf_token %} 
     {% if form.email.errors %}`enter code here` 
     <div class="message red-gradient">{{ form.email.errors }}</div><br/> 
     {% endif %} 

     <p>E-mail address: {{ form.email }} <br/><br/> 
    <input type="submit" value="Reset my password"></p> 
    </form> 

{% endblock content %}