djoser通过链接激活帐户

时间:2019-12-03 07:30:26

标签: django python-3.x django-rest-framework django-rest-framework-jwt djoser

点击djoser发送的链接后如何激活? 我的设置 '''

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'djoser',
    'rest_framework',
    'rest_framework_simplejwt',
    'data',

]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES':(
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
}

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'technomancer7629@gmail.com'
EMAIL_HOST_PASSWORD='naz@technomancer7629'
EMAIL_PORT = 587

PROTOCOL = "http"
DOMAIN = "127.0.0.1:8000"
DJOSER = {
    'PASSWORD_RESET_CONFIRM_URL': '/password/reset/confirm/{uid}/{token}',
    'USERNAME_RESET_CONFIRM_URL': '/username/reset/confirm/{uid}/{token}',
    'ACTIVATION_URL': 'auth/user/activate/{uid}/{token}',
    'SEND_ACTIVATION_EMAIL': True,
    'SEND_CONFRIMATION_EMAIL':True,
    'SERIALIZERS': {},
    'EMAIL':{
        'activation': 'djoser.email.ActivationEmail',
    },
}

''' urls.py '''

urlpatterns = [
    path('admin/', admin.site.urls),
    path('auth/',include('djoser.urls')),
    path('auth/',include('djoser.urls.jwt')),
    path("api/data/",include("data.urls")),


]

''' 我的电子邮件链接 http://127.0.0.1:8000/auth/users/activate/Mjc/5bx-5f9542251fd9db7e980b 错误:

Django使用startgo1.urls中定义的URLconf,按以下顺序尝试了以下URL模式:

admin/
auth/ ^users/$ [name='user-list']
auth/ ^users\.(?P<format>[a-z0-9]+)/?$ [name='user-list']
auth/ ^users/activation/$ [name='user-activation']
auth/ ^users/activation\.(?P<format>[a-z0-9]+)/?$ [name='user-activation']
auth/ ^users/me/$ [name='user-me']
auth/ ^users/me\.(?P<format>[a-z0-9]+)/?$ [name='user-me']
auth/ ^users/resend_activation/$ [name='user-resend-activation']
auth/ ^users/resend_activation\.(?P<format>[a-z0-9]+)/?$ [name='user-resend-activation']
auth/ ^users/reset_password/$ [name='user-reset-password']
auth/ ^users/reset_password\.(?P<format>[a-z0-9]+)/?$ [name='user-reset-password']
auth/ ^users/reset_password_confirm/$ [name='user-reset-password-confirm']
auth/ ^users/reset_password_confirm\.(?P<format>[a-z0-9]+)/?$ [name='user-reset-password-confirm']
auth/ ^users/reset_username/$ [name='user-reset-username']
auth/ ^users/reset_username\.(?P<format>[a-z0-9]+)/?$ [name='user-reset-username']
auth/ ^users/reset_username_confirm/$ [name='user-reset-username-confirm']
auth/ ^users/reset_username_confirm\.(?P<format>[a-z0-9]+)/?$ [name='user-reset-username-confirm']
auth/ ^users/set_password/$ [name='user-set-password']
auth/ ^users/set_password\.(?P<format>[a-z0-9]+)/?$ [name='user-set-password']
auth/ ^users/set_username/$ [name='user-set-username']
auth/ ^users/set_username\.(?P<format>[a-z0-9]+)/?$ [name='user-set-username']
auth/ ^users/(?P<pk>[^/.]+)/$ [name='user-detail']
auth/ ^users/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='user-detail']
auth/ ^$ [name='api-root']
auth/ ^\.(?P<format>[a-z0-9]+)/?$ [name='api-root']
auth/ ^jwt/create/? [name='jwt-create']
auth/ ^jwt/refresh/? [name='jwt-refresh']
auth/ ^jwt/verify/? [name='jwt-verify']
api/data/

当前路径auth / users / activate / Mjc / 5bx-5f9542251fd9db7e980b与任何这些都不匹配。

3 个答案:

答案 0 :(得分:1)

用 djoser 试试这个:

在 auth.urls 中: path('activate/<str:uid>/<str:token>/', ActivateUserEmail.as_view(), name='activate email')

`class ActivateUserEmail(APIView)`:
    `def get (self, request, uid, token)`:
        `protocol = 'https://' if request.is_secure() else 'http://'`
        `web_url = protocol + request.get_host()`
        `post_url = web_url + "/auth/users/activation/"`
        `post_data = {'uid': uid, 'token': token}`
        `result = requests.post(post_url, data = post_data)`
        `message = result.text`
        `return Response(message)`

答案 1 :(得分:0)

我通读了Djoser的docs,您的激活URL正确,但是您使用了错误的方式,您应该将该URL与POST请求一起使用才能使其正常工作(当前您单击它的方式)将调用GET请求),因此我建议在Django上创建一个URL端点以处理GET请求,以便在this issue之后发送POST请求。

在您的urls.py中:

path('activate/<str:uid>/<str:token>/', UserActivationView.as_view()),

然后您views.py将处理它并在URL上调用POST请求:

class UserActivationView(APIView):
    def get (self, request, uid, token):
        protocol = 'https://' if request.is_secure() else 'http://'
        web_url = protocol + request.get_host()
        post_url = web_url + "/auth/users/activate/"
        post_data = {'uid': uid, 'token': token}
        result = requests.post(post_url, data = post_data)
        content = result.text()
        return Response(content)

答案 2 :(得分:0)

谢谢您。我刚刚用这段代码解决了。

def ActivateUserAccount(request, uidb64=None,token=None):
 #print(force_text(urlsafe_base64_decode(uidb64)))
    #print(token)
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        #print(type(uid),uid)
        user = User.objects.get(pk=uid)
        print(user)
    except User.DoesNotExist:
        user = None
    if user and default_token_generator.check_token(user,token):
        user.is_email_verified = True
        user.is_active = True
        user.save()
        login(request,user)
        print("Activaton done")
    else:
        print("Activation failed")