Contrib Auth重置密码错误

时间:2016-05-27 23:46:14

标签: python django

我在django中使用contrib auth reset_password,当我点击它发送给我的链接时出现错误。

[27/May/2016 20:23:41] ERROR [django.request:256] Internal Server Error: /reset/MQ/4c9-5eacbebedfa0c1742a1c/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 164, in get_response
    response = response.render()
  File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 158, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 135, in rendered_content
    content = template.render(context, self._request)
  File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py", line 74, in render
    return self.template.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 209, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 135, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 65, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 507, in render
    six.reraise(*exc_info)
  File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 493, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 579, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 496, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'password-reset-confirm' with arguments '()' and keyword arguments '{u'uidb64': '', u'token': ''}' not found. 1 pattern(s) tried: ['reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']

这是我的urls.py:

url(r'^password_reset/$', views.reset, name='password-reset'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
    views.reset_confirm, name='password-reset-confirm'),

和我的views.py:

def reset_confirm(request, uidb64=None, token=None):
    return password_reset_confirm(request, template_name='registration/reset_confirm.html',
        uidb64=uidb64, token=token, post_reset_redirect=reverse('dashboard-login'))


def reset(request):
    return password_reset(request, template_name='registration/reset.html',
        email_template_name='registration/reset_email.html',
        subject_template_name='registration/reset_subject.txt',
        post_reset_redirect=reverse('dashboard-login'))

和电子邮件模板:

Someone asked for password reset for email {{ email }}. Follow the link below:
https://www.websitename.com{% url 'password-reset-confirm' uidb64=uid token=token %}

是的,实际模板中不是“websitename.com”。和我的重置页面模板:

<div class="reset-page">
        <h3 class="reset-header">{% blocktrans %}Reset Password - Step 1 of 2{% endblocktrans %}</h3>
        <form class="login-form" action="{% url 'password-reset' %}" method="post"> 
            <div class='form'>                
                {% csrf_token %}               
                <input id="id_email" name="email" type="text" class="text-login" placeholder="Email" />
                <input type="submit" class="submit-login" style="margin-top: 30px;" value="{% trans 'Submit' %}" />
            </div>
        </form>
        <p class="reset-info">{% blocktrans %}Enter your email address to receive a password change link.{% endblocktrans %}</p>
</div>  

和未显示的模板,确认:

<div class="reset-page">
        <h3 class="reset-header">{% blocktrans %}Reset Password - Step 2 of 2{% endblocktrans %}</h3>
        <form class="login-form" action="{% url 'password-reset-confirm' uidb64=uidb64 token=token %}" method="post">
            <div class='form'>
                {% if validlink %}
                    {% csrf_token %}
                    <input id="id_new_password1" name="new_password1" type="text" class="text-login" placeholder="Password" />
                    <input id="id_new_password2" name="new_password2" type="text" class="text-login" placeholder="Confirm Password" />
                    <input type="submit" class="submit-login" value="{% trans 'Submit' %}" />
                {% else %}
                    <p class="reset-error">{% blocktrans %}Error: This reset link is no longer valid!{% endblocktrans %}</p>
                {% endif %}
                {% if error_messages %}
                    <p class="reset-error">Error: {{ error_messages }}</p>
                {% endif %}                    
            </div>
        </form>
        <p class="reset-info">{% blocktrans %}Enter your new password, twice.{% endblocktrans %}</p>
</div>

它适用于电子邮件的链接。我在/ password_reset /上输入我的电子邮件并发送电子邮件,但链接给了我这个错误,而不是带我到我输入2个新密码的页面。错误似乎发生在确认模板中的url标记中。

编辑:

这是django.contrib.auth.views.password_reset_confirm(),它是错误来源的一部分:

@sensitive_post_parameters()
@never_cache
def password_reset_confirm(request, uidb64=None, token=None,
                           template_name='registration/password_reset_confirm.html',
                           token_generator=default_token_generator,
                           set_password_form=SetPasswordForm,
                           post_reset_redirect=None,
                           current_app=None, extra_context=None):
    """
    View that checks the hash in a password reset link and presents a
    form for entering a new password.
    """
    UserModel = get_user_model()
    assert uidb64 is not None and token is not None  # checked by URLconf
    if post_reset_redirect is None:
        post_reset_redirect = reverse('password_reset_complete')
    else:
        post_reset_redirect = resolve_url(post_reset_redirect)
    try:
        # urlsafe_base64_decode() decodes to bytestring on Python 3
        uid = force_text(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 token_generator.check_token(user, token):
        validlink = True
        title = _('Enter new password')
        if request.method == 'POST':
            form = set_password_form(user, request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(post_reset_redirect)
        else:
            form = set_password_form(user)
    else:
        validlink = False
        form = None
        title = _('Password reset unsuccessful')
    context = {
        'form': form,
        'title': title,
        'validlink': validlink,
    }
    if extra_context is not None:
        context.update(extra_context)

    if current_app is not None:
        request.current_app = current_app

    return TemplateResponse(request, template_name, context)

我意识到在确认模板中没有得到参数uidb64和token。所以我添加了它们,但现在我在顶部得到的错误略有不同。争论似乎是空洞的。我怎样才能获得uidb64和token的值,以便将它们传递给url?

1 个答案:

答案 0 :(得分:0)

解决方案是将确认模板中的表单操作设置为

action=""
奇怪的是。