简体   繁体   English

贡献验证重置密码错误

[英]Contrib Auth Reset Password Error

I am using contrib auth reset_password in django and I get an error when I click the link it sends me. 我在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})/$']

Here is my urls.py: 这是我的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'),

and my views.py: 和我的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'))

and the email template: 和电子邮件模板:

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

yes it isn't "websitename.com" in the actual template. 是的,实际模板中不是“ websitename.com”。 and my reset page templates: 和我的重置页面模板:

<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>  

and the template that isn't showing, the confirm: 以及未显示的模板,请确认:

<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>

It works up to the email's link. 它可以处理电子邮件的链接。 I enter my email on /password_reset/ and it sends the email, but the link gives me that error instead of taking me to the page where I enter 2 new passwords. 我在/ password_reset /上输入我的电子邮件,它发送电子邮件,但是该链接给了我该错误,而不是带我到输入2个新密码的页面。 The error seems to occur in the url tag in the confirm template. 该错误似乎发生在确认模板的url标记中。

EDIT: 编辑:

Here is django.contrib.auth.views.password_reset_confirm() which is part of where the error comes from: 这是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)

I realized that in the confirm template it wasn't getting the arguments uidb64 and token. 我意识到在确认模板中并没有得到uidb64和token参数。 So I added them but now I am getting the error at the top which is slightly different. 所以我添加了它们,但是现在我在顶部得到了错误,该错误略有不同。 The arguments seem to be empty. 参数似乎是空的。 How can I get the values of uidb64 and token so I can pass them into the url? 如何获取uidb64和token的值,以便可以将它们传递到url中?

The solution was to set form action in the confirm template to 解决方案是将确认模板中的表单操作设置为

action=""

oddly enough. 说来也怪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “AttributeError: module 'django.contrib.auth.views' has no attribute 'password_reset'” urls.py 中的错误 - “AttributeError: module 'django.contrib.auth.views' has no attribute 'password_reset' ” error in urls.py 路径已弃用(django.contrib.auth.views.password_reset_confirm) - path is deprecated (django.contrib.auth.views.password_reset_confirm) NoReverseMatch:反向为``django.contrib.auth.views.password_reset_confirm&#39;&#39; - NoReverseMatch: Reverse for ''django.contrib.auth.views.password_reset_confirm'' password_reset行185中的&#39;str&#39;对象不可调用/django/contrib/auth/views.py - 'str' object is not callable /django/contrib/auth/views.py in password_reset, line 185 为什么django 1.6在contrib.auth.views.password_reset_confirm视图上将“ form”设置为“ none”? - Why is django 1.6 setting “form” to “none” on the contrib.auth.views.password_reset_confirm view? Django (v3.1) PASSWORD RESET LINK: AssertionError at line 260 of django/contrib/auth/views.py assert 'uidb64' in kwargs and 'token' in kwargs - Django (v3.1) PASSWORD RESET LINK: AssertionError at line 260 of django/contrib/auth/views.py assert 'uidb64' in kwargs and 'token' in kwargs 为什么在 django rest-auth 密码重置中密码重置不成功 - Why Password reset unsuccessful in django rest-auth password reset 使用 rest_auth 在 Django 中重置密码 - Password Reset in Django with rest_auth NoReverseMatch 在 /rest-auth/password/reset/ - NoReverseMatch at /rest-auth/password/reset/ Django REST-Auth密码重置 - Django REST-Auth Password Reset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM