使用Djoser和Django Rest Framework激活帐户 [英] Accounts activation with Djoser and Django Rest Framework
问题描述
我在我的项目中使用Djoser进行身份验证。
现在已经努力添加电子邮件激活超过4天了,但是由于文档有点让我难以理解,因此似乎没能抓住它。
Am using Djoser for authentication in my project. Have been struggling to add email activation for over 4 days now but seems have failed to grab it fine as the documentation is a little hard for me to understand.
这是我的代码
settings.py
#change auth model to custom model
AUTH_USER_MODEL = 'userauth.User'
#setting up email server
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'okumujustine01@gmail.com'
EMAIL_HOST_PASSWORD = 'codemanuzmaster'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'okumujustine01@gmail.com'
#djoser login settings
DJOSER = {
'DOMAIN': 'localhost:8000',
'SITE_NAME': 'net',
'LOGIN_FIELD':'email',
'USER_CREATE_PASSWORD_RETYPE':True,
'ACTIVATION_URL': '#/users/activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'SERIALIZERS':{
'user_create':'userauth.serializers.UserCreateSerializer',
'user':'userauth.serializers.UserCreateSerializer',
'activation': 'djoser.email.ActivationEmail',
}
}
这是我在创建用户后收到的电子邮件
here is the email i receive after creating user
http://example.com/auth/users/activate/MQ/5c9-26bcab9e85e8a967731d
它显示 example.com ,但是我希望它将网址更改为 localhost:8000
It shows example.com but i want it to change the web url to localhost:8000 instead
您正在接收此电子邮件,因为您需要在example.com上完成激活过程。
You're receiving this email because you need to finish activation process on example.com.
请转到以下内容页面以激活帐户:
Please go to the following page to activate account:
http://example.com/auth/users/activate/MQ/5c9-26bcab9e85e8a967731d
如果我手动将网址更改为
And if i change the web url manually to
http://127.0.0.1:8000/users/activate/MQ/5c9-26bcab9e85e8a967731d
它不断返回
{
"detail": "Authentication credentials were not provided."
}
我真的要求你们帮助我。
I really request you people to help me.
推荐答案
要回答您的原始问题(如何为激活URL使用不同的域),您需要从Djoser设置中删除DOMAIN和SITE_NAME。示例:
To answer your original question (How to use a different domain for the activation url), you need to remove DOMAIN and SITE_NAME from the Djoser settings. Example:
DOMAIN = config('DOMAIN') #localhost:8000
SITE_NAME = config('SITE_NAME') #net
DJOSER = {
'LOGIN_FIELD':'email',
'USER_CREATE_PASSWORD_RETYPE':True,
'ACTIVATION_URL': '#/users/activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'SERIALIZERS':{
'user_create':'userauth.serializers.UserCreateSerializer',
'user':'userauth.serializers.UserCreateSerializer',
'activation': 'djoser.email.ActivationEmail',
}
然后您应该在电子邮件中获得下一个链接:
Then you should get the next link in your email:
http://localhost:8000/auth/users/activate/MQ/5c9-26bcab9e85e8a967731d
这篇关于使用Djoser和Django Rest Framework激活帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!