django

时间:2017-08-24 09:12:52

标签: python django

如果激活网址错误或无效,我收到404错误。

accounts / urls.py -

urlpatterns = [
    url(r"^signup/$", views.signup, name="account_signup"),
    url(r'^login/$', views.login_view, name='account_login'),
    url(r'^logout/$', auth_views.logout, {'next_page': '/account/login'}, name='logout'),
    url(r'^confirmemail/$', views.confirmemail, name='account_confirmemail'),
    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'),
]

账户/ 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.profile.email_confirmed = True
        user.save()
        user.profile.save()
        login(request, user)
        return redirect('home')
    else:
        return render(request, 'accounts/account_activation_invalid.html')
        #return HttpResponseRedirect("account_activation_invalid.html")

如果帐户激活网址错误,则应重定向到account_activation_invalid.html

非常感谢任何帮助/链接。提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你可以试试:

在urls.py中

添加:

url(r'^activate/',views.activate, name='empty_activate')

在视图中将definitio更改为:

def activate(request, uidb64=None, token=None):