1
votes

How to activate after click on the link send by djoser? my settings '''

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 = '[email protected]'
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")),


]

''' my email link http://127.0.0.1:8000/auth/users/activate/Mjc/5bx-5f9542251fd9db7e980b error:

Using the URLconf defined in startgo1.urls, Django tried these URL patterns, in this order:

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/

The current path, auth/users/activate/Mjc/5bx-5f9542251fd9db7e980b, didn't match any of these.

3
are you sure that you suppose to add url with users/activation/ and not users/activate/ to the email link?Linh Nguyen
i dont kno. check my activation url .Is it wrong?safakat001
i did not add those urls djoser add themsafakat001

3 Answers

1
votes

I read through the docs of Djoser and your activation URL is correct but you using it the wrong way, you supposed to use that URL with a POST request for it to work( currently the way you click on it will call a GET request) so i suggest creating a URL endpoint on Django to handle GET request to send POST request of it following this issue .

In your urls.py:

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

And you views.py will handle it and call POST request on URL:

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)
1
votes

With djoser try this:

in 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)`
0
votes

Thanks for your. I just solved it with this code.

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")