django-rest-auth: 邮箱验证有什么用,如何让用户正确激活账户?
django-rest-auth: What is the email verification for and how can I let the user activate the account correctly?
我可能误会了什么。 django-rest-auth
提供 RegisterView
并且应该在用户注册后将电子邮件发送到用户的电子邮件。但是,我注意到用户甚至在用户确认电子邮件之前就已经注册了 is_active=True
。那么邮箱验证有什么用呢?当用户在网站上注册时,我是否必须覆盖某些内容并使用 is_active=False
创建用户?如果是这样,我怎样才能正确地实现它?我遇到了麻烦。
这是我所做的。
urls.py
path('api/v1/rest-auth/registration/', include('rest_auth.registration.urls')),
serializers.py
from rest_auth.registration.serializers import RegisterSerializer
class CustomRegistrationSerializer(RegisterSerializer):
def save(self, request):
user = super(CustomRegistrationSerializer, self).save(request)
user.is_active = False
return user
settings.py
REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'appname.serializers.CustomRegistrationSerializer',
}
这是我遇到的一个错误。
File "rest_auth/registration/views.py", line 46, in dispatch
return super(RegisterView, self).dispatch(*args, **kwargs)
File "/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "rest_framework/generics.py", line 192, in post
return self.create(request, *args, **kwargs)
File "rest_auth/registration/views.py", line 65, in create
user = self.perform_create(serializer)
File "rest_auth/registration/views.py", line 81, in perform_create
None)
File "allauth/account/utils.py", line 183, in complete_signup
signal_kwargs=signal_kwargs)
File "/allauth/account/utils.py", line 133, in perform_login
return adapter.respond_user_inactive(request, user)
File "allauth/account/adapter.py", line 454, in respond_user_inactive
reverse('account_inactive'))
File "django/urls/resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'account_inactive' not found. 'account_inactive' is not a valid view function or pattern name.
我想听听使用 django-rest-auth
的人如何处理注册。
有人用过 django-rest-auth
可以给我提示吗?
is_active
其实不是为了这个目的。当我不想删除他时,我将它用作管理员来停用用户。您可能想改用 ACCOUNT_EMAIL_VERIFICATION = "mandatory"
:
ACCOUNT_EMAIL_VERIFICATION (=”optional”)
Determines the e-mail verification method during signup – choose one of "mandatory"
, "optional"
, or "none"
. When set to “mandatory” the user is blocked from logging in until the email address is verified. Choose “optional” or “none” to allow logins with an unverified e-mail address. In case of “optional”, the e-mail verification mail is still sent, whereas in case of “none” no e-mail verification mails are sent.
来源:https://django-allauth.readthedocs.io/en/latest/configuration.html
许多网站需要双重确认才能验证电子邮件地址的有效性或出于法律目的,您可以使用引用设置来完成此操作。
关于NoReverseMatch
异常,你应该可以用下面的代码解决它(但我认为当你切换到ACCOUNT_EMAIL_VERIFICATION = "mandatory"
时你可能不需要它):
urlpatterns = [
...
url(r'^accounts/', include('allauth.urls')),
...
]
文档:https://django-allauth.readthedocs.io/en/latest/installation.html
但是,如果 Django 只是一个后端,您可能不需要此视图或不想覆盖它。要了解如何做到这一点,您可以查看我的 gist,我也覆盖了适配器,因为有一个 URL 我不需要。
django rest auth 通过使用额外的 table 来处理电子邮件,这些电子邮件帐户和标志是否经过验证..
它取自 django-allauth。
我可能误会了什么。 django-rest-auth
提供 RegisterView
并且应该在用户注册后将电子邮件发送到用户的电子邮件。但是,我注意到用户甚至在用户确认电子邮件之前就已经注册了 is_active=True
。那么邮箱验证有什么用呢?当用户在网站上注册时,我是否必须覆盖某些内容并使用 is_active=False
创建用户?如果是这样,我怎样才能正确地实现它?我遇到了麻烦。
这是我所做的。
urls.py
path('api/v1/rest-auth/registration/', include('rest_auth.registration.urls')),
serializers.py
from rest_auth.registration.serializers import RegisterSerializer
class CustomRegistrationSerializer(RegisterSerializer):
def save(self, request):
user = super(CustomRegistrationSerializer, self).save(request)
user.is_active = False
return user
settings.py
REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'appname.serializers.CustomRegistrationSerializer',
}
这是我遇到的一个错误。
File "rest_auth/registration/views.py", line 46, in dispatch return super(RegisterView, self).dispatch(*args, **kwargs)
File "/rest_framework/views.py", line 495, in dispatch response = self.handle_exception(exc)
File "rest_framework/views.py", line 455, in handle_exception self.raise_uncaught_exception(exc)
File "rest_framework/views.py", line 492, in dispatch response = handler(request, *args, **kwargs)
File "rest_framework/generics.py", line 192, in post return self.create(request, *args, **kwargs)
File "rest_auth/registration/views.py", line 65, in create user = self.perform_create(serializer)
File "rest_auth/registration/views.py", line 81, in perform_create None)
File "allauth/account/utils.py", line 183, in complete_signup signal_kwargs=signal_kwargs)
File "/allauth/account/utils.py", line 133, in perform_login return adapter.respond_user_inactive(request, user)
File "allauth/account/adapter.py", line 454, in respond_user_inactive reverse('account_inactive'))
File "django/urls/resolvers.py", line 622, in _reverse_with_prefix raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'account_inactive' not found. 'account_inactive' is not a valid view function or pattern name.
我想听听使用 django-rest-auth
的人如何处理注册。
有人用过 django-rest-auth
可以给我提示吗?
is_active
其实不是为了这个目的。当我不想删除他时,我将它用作管理员来停用用户。您可能想改用 ACCOUNT_EMAIL_VERIFICATION = "mandatory"
:
ACCOUNT_EMAIL_VERIFICATION (=”optional”)
Determines the e-mail verification method during signup – choose one of
"mandatory"
,"optional"
, or"none"
. When set to “mandatory” the user is blocked from logging in until the email address is verified. Choose “optional” or “none” to allow logins with an unverified e-mail address. In case of “optional”, the e-mail verification mail is still sent, whereas in case of “none” no e-mail verification mails are sent.
来源:https://django-allauth.readthedocs.io/en/latest/configuration.html
许多网站需要双重确认才能验证电子邮件地址的有效性或出于法律目的,您可以使用引用设置来完成此操作。
关于NoReverseMatch
异常,你应该可以用下面的代码解决它(但我认为当你切换到ACCOUNT_EMAIL_VERIFICATION = "mandatory"
时你可能不需要它):
urlpatterns = [
...
url(r'^accounts/', include('allauth.urls')),
...
]
文档:https://django-allauth.readthedocs.io/en/latest/installation.html
但是,如果 Django 只是一个后端,您可能不需要此视图或不想覆盖它。要了解如何做到这一点,您可以查看我的 gist,我也覆盖了适配器,因为有一个 URL 我不需要。
django rest auth 通过使用额外的 table 来处理电子邮件,这些电子邮件帐户和标志是否经过验证..
它取自 django-allauth。