python - Django 重新发送激活链接,缺少 1 个必需的位置参数 : 'user'

标签 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'))

accounts/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'),

这是将请求用户参数传递给函数的正确方法吗?

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

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

最佳答案

将你的 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>

关于python - Django 重新发送激活链接,缺少 1 个必需的位置参数 : 'user' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56680485/

相关文章:

python - 匹配Django中所有没有前缀的url

regex - Django url 正则表达式命中 url : *./something 的结尾

python - Django __unicode__ 和 FK 非常慢

python - 如何在 Django 中生成 303 Http 响应?

django - 在 Django 中以编程方式执行嵌套 GET 请求

python - Tweepy 和 SocialRegistration 问题

python - 有效管理日益复杂的 Django URL View ? (网址.py)

python - 为什么使用 __iter__() 方法在实例上调用 list() 会导致递归?

python - 重复减去字符串和datetime.time

python - 名称错误 : global name 'loader' is not defined