django-rest-auth处理已过期的确认电子邮件 [英] django-rest-auth handling expired confirmation email
问题描述
我正在使用django-rest-auth和django-allauth在我的rest api中处理用户身份验证.当用户在链接过期后尝试验证其电子邮件时,出现错误页面.
I am using django-rest-auth and django-allauth to handle user authentication in my rest api. When a user tries to verify their email after the link has expired, I get an unpleasant error page.
请,我如何显示一个更好的错误页面或发送一条消息,告诉他们由于链接已过期而失败?
Please, how can I display a better error page or send a message telling them it wasn't successful because the link had expired?
推荐答案
据我了解,您的错误来自 django-allauth
,而不是您的项目.错误的原因是您没有在主 urls.py
中包含 allauth.url
(将在后面的部分中进行详细说明).
As far as I can understand, your error is coming from django-allauth
, not from your project. Reason for the error is that you did not include allauth.url
in your main urls.py
(will explain more in later section).
在 urls.py
中添加 allauth.urls
:
urlpatterns = [
...
path('accounts/', include('allauth.urls')),
...
]
第二个解决方案
如果您深入研究问题,您将看到错误提示 NoReverseMatch
错误,该错误是在项目中找不到URL名称(即 account_login
)时发生的>.现在,此错误来自模板,位于 allauth基本模板
.
Second Solution
If you go deep into the problem, you will see the error is saying NoReverseMatch
error, which occurs when there is a url name which is not found in project ie account_login
. Now this error is coming from template at at allauth base template
.
根据您的代码判断,由于以下几行而发生此错误:(我冒昧检查了您的github代码库,因为它是开源的.)
Judging by your code, this error is occurring due to these line: (I took the liberty to check your github codebase as it was opensource.)
if not email_confirmation:
if queryset is None:
queryset = self.get_queryset()
try:
email_confirmation = queryset.get(key=key.lower())
except EmailConfirmation.DoesNotExist:
# A React/Vue Router Route will handle the failure scenario
return HttpResponseRedirect('/login/failure/') # <-- Here
它指向系统中不存在的错误URL.请检查 django-rest-身份验证网址
.
It is pointing to a wrong url which does not exist in system. Please check django-rest-auth urls
.
因此,此处的一种解决方法是在此处提供具有以下视图的模板响应:
So one fix here is to provide a template response here with a view like this:
# in accounts/api/urls.py
path('failure/', TemplateView.as_view(template_name='api_failure.html'))
另一种解决方案是提供 自定义404模板
,如下所示:
Another solution is to provide custom 404 template
like this:
# accounts/api/views.py
def handler404(request, exception, template_name="your_custom_404.html"):
response = render_to_response(template_name)
response.status_code = 404
return response
# root urls.py
handler404 = 'accounts.api.views.handler404'
这篇关于django-rest-auth处理已过期的确认电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!