【问题标题】:User account activation url not found in Django在 Django 中找不到用户帐户激活 url
【发布时间】:2021-05-30 01:32:27
【问题描述】:

我正在尝试在 django 中激活用户帐户,但出现 404 错误,该错误是由于 django 无法获取激活 url。我正在尝试评估的链接是 http://127.0.0.1:8000/account/activate/MTU/anfs0f-046aa586847b91da0546e995f34c392a,但我一直找不到 url。

django app urls.py
urlpatterns = [
path('account/', include('account.urls', namespace='account')), 
]
================
urls.py
urlpatterns = [
path('activate/<slug:uidb64>/<slug:token>)/', views.account_activate, name='activate'),
]

views.py

def account_activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = UserBase.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 redirect('account:dashboard')
    else:
        return render(request, 'account/registration/activation_invalid.html')

模板

{% autoescape off %}
Great {{ user.user_name }}!
Please click on the link below to activate your account
http://{{ domain }}{% url 'account:activate' uidb64=uid token=token %}
{% endautoescape %}

我花了几个小时试图找出我的错误在哪里,但我没能做到。请我对此有所了解。

【问题讨论】:

  • 地址栏中显示浏览器的网址是什么?也许您创建了错误的网址。你真的需要网址中的http://{{ domain }} 吗?你确定它必须是'account:activate' - 或者可能是'activate'
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • 生成的链接是http://127.0.0.1:8000/account/activate/MTU/anfs0f-046aa586847b91da0546e995f34c392a
  • 再次阅读我的评论 - 提出问题,而不是评论。更多人会看到它,以便更多人可以帮助您。
  • 我不知道是什么问题。我们无法运行它,所以我们无法测试它。您只能从更简单的版本开始,并检查它是否适用于 path('activate,...)`。我假设你它是应用程序文件夹中的urls.py,你也将它导入到设置文件夹中的urls.py

标签: python django token


【解决方案1】:

我认为您应该将您的 app_name 放在 urlpatterns 顶部的帐户 url 中。

【讨论】:

  • 我将 app_name 添加到我拥有帐户 url 的文件中,但问题仍然存在
  • 请求时在您的网址末尾添加一个“/”。
猜你喜欢
  • 2016-08-31
  • 2016-03-11
  • 2020-08-08
  • 2020-02-15
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多