Django重新发送激活链接,缺少1个必需的位置参数:“用户”

时间:2019-06-20 06:49:54

标签: python django django-urls django-users django-request

我已经阅读了类似的问题,但是我不明白为什么这不起作用。我正在尝试向用户单击链接时发送重新激活电子邮件。当用户注册并发送电子邮件时,会正确生成激活链接,但是当我尝试再次调用相同的功能来重新激活链接时,表示缺少一个参数是行不通的。功能如下:

acounts / views.py

def sendactivationmail(request, user):
    # Creating token and masing uid of user
    token = default_token_generator.make_token(user)
    uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
    # Sending email for email verification
    subject = 'Please Verify Your Email'
    from_email = settings.DEFAULT_FROM_EMAIL
    to_email = [user.email]
    context = {
        'url': settings.BASE_URL + reverse('user-activation-link', kwargs={
            'uidb64': uid,
            'token': token,
        }),
    }
    contact_message = render_to_string('verify_email.html', context)
    plain_message = strip_tags(contact_message)
    send_mail(subject, plain_message, from_email, to_email, html_message=contact_message, fail_silently=True)

    return redirect(reverse('login'))

帐户/urls.py

from django.conf.urls import url
from .views import *
from . import views
from urllib import request
from django.contrib.auth.models import User


urlpatterns = [
    url(r'^reactivate/$', views.sendactivationmail(request, User), name='reactivate'),

这是将 request user 参数传递给函数的正确方法吗?

编辑:这是用于重定向用户的链接:

<p><a href="{% url 'reactivate' %}">Click here</a> to resend the activation email.</p>

1 个答案:

答案 0 :(得分:1)

将您的urlpattern更改为此

urlpatterns = [
    url(r'^reactivate/$', views.sendactivationmail, name='reactivate'),
    .....
]

和html到,但是用户必须登录才能使用此链接。记住那个

<p><a href="{% url 'reactivate' request.user %}">Click here</a> to resend the activation email.</p>