1

单击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 错误:

使用 startgo1.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 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 与其中任何一个都不匹配。

4

3 回答 3

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)
于 2021-07-20T12:52:13.283 回答
1

我通读了Djoser 的文档 ,您的激活 URL 是正确的,但是您以错误的方式使用它,您应该将该 URL 与 POST 请求一起使用以使其工作(当前您单击它的方式将调用 GET 请求)所以我建议在 Django 上创建一个 URL 端点来处理 GET 请求,以便在此问题之后发送它的 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)
于 2019-12-03T07:54:13.613 回答
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")
于 2019-12-03T08:35:56.503 回答