1

我在 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'),)

我的意见.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) 

不知道如何解决这个问题。需要一些指导...

4

4 回答 4

5

密码重置 Django 有一个我们将要使用的内置密码重置功能。在本教程中,我们将我们的应用程序命名为帐户。

网址.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()

网址.py

from django.conf.urls import *

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

视图.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 %}

设置.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 %}
于 2013-12-30T11:37:47.127 回答
2

比您的 def lexresetpasswordconfirmed(request): 还应该接受 uidb36 和令牌参数。

于 2012-07-16T10:05:36.973 回答
1
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{'uidb36': '1', 'token': '392-804fab6dcec790f0ec6b'}' not found.

这意味着在执行的某个时刻,您不是在调用 reverse on lexresetpasswordconfirmed,而是在调用它 on django.contrib.auth.views.password_reset_confirm

这个错误发生在哪里?在模板中?如果是这样,请确保您使用的模板具有

{% url lexresetpasswordconfirmed uid token %}

并不是

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

错误是否发生在视图中?如果是这样,您在某处调用 reverse on django.contrib.auth.views.password_reset_confirm

一旦该错误得到解决,那么是的,您将不得不解决 Alexander 指出的另一个错误,即在视图函数中包含 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——并不关心你是否为它提供了重定向,它正在使用它自己的。

于 2012-07-16T16:28:19.447 回答
0

我的设置.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')

我的 urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', user_views.register,name='register'),
    path('profile/', user_views.profile,name='profile'),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'),name='login'),
    path('logout/',auth_views.LogoutView.as_view(template_name='users/logout.html'),name='logout'),
    path('password-reset/',auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),name='password_reset'),
    path('password-reset/done/',auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),name='password_reset_done'),
    path('password-reset-confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),name='password_reset_confirm'),

    path('password-reset-complete/',
         auth_views.PasswordResetCompleteView.as_view(
             template_name='users/password_reset_complete.html'
         ),
         name='password_reset_complete'),
    path('', include('blog.urls')),
]

密码重置.html:

    {% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
           <legend class="border-bottom mb-4">Reset Password</legend>
           {{ form|crispy }}
        </fieldset>
        <div class="form-group">
            <button class="btn btn-outline-info" type="submit">Request Password Reset</button>
        </div>
     </form>
  </div>
{% endblock content %} 

密码重置完成:

    {% extends "blog/base.html" %}
{% block content %}
    <div class="alert alert-info">
        An email has been sent with instructions to reset your password
    </div>
{% endblock content %} 

密码重置确认.html:

    {% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
           <legend class="border-bottom mb-4">Reset Password</legend>
           {{ form|crispy }}
        </fieldset>
        <div class="form-group">
            <button class="btn btn-outline-info" type="submit">Reset Password</button>
        </div>
     </form>
  </div>
{% endblock content %} 

password_reset_complete.html:

    {% extends "blog/base.html" %}
{% block content %}
    <div class="alert alert-info">
        Your password has been set.
    </div>
    <a href="{% url 'login' %}">Sign In Here</a>
{% endblock content %}
于 2019-04-07T20:47:00.330 回答