简体   繁体   中英

can't authenticate user in django

I create authentication link to email the user and this is the error I get: Reverse for 'activate' with keyword arguments '{'uidb64': 'MzA', 'token': 'a8o36j-51326a9d14c0ebf00a88a9a8c4983014'}' not found. 1 pattern(s) tried: ['activate/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$'] Reverse for 'activate' with keyword arguments '{'uidb64': 'MzA', 'token': 'a8o36j-51326a9d14c0ebf00a88a9a8c4983014'}' not found. 1 pattern(s) tried: ['activate/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']

urls.py:

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

line from views.py:

message = render_to_string('main/acc_active_email.html'

activate function in views.py:

def activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None
    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.save()
        login(request, user)
        return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
    else:
        return HttpResponse('Activation link is invalid!')

acc_active_email.html:

{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'main:activate' uidb64=uid token=token %}
{% endautoescape %}

Now it may be because I copied it from an article that refrences an older version of django. any help is apperciated!

From your urls.py

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

note the regex pattern you use for the token:

[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20}

This means that it will match for a string of "1-13 alphanumeric (english) characters, followed by a dash, followed by 1-20 alphanumeric characters". If you look at your token, a8o36j-51326a9d14c0ebf00a88a9a8c4983014 , you can see that it actually has 32 characters following the dash. So you need to replace the 20 with 32 .

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

I've tried the reverse call you describe, and it now seems to work.

I'm guessing that somewhere in django's history, and/or the package you're relying on, they increased the token algorithms (to improve security, I assume) so that they produce longer tokens. I couldn't find a description of this, or when it happened, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM