5

I am using the Django rest framework and Djoser for Authentication and User Registration.

When a new user registers, Djoser sends an activation email with a link that does a GET request. In order to activate, I need to extract the uid and token from the activation URL and make a POST request for Djoser to be able to activate the user.

My environment is Python 3 and Django 1.11, Djoser 1.0.1.

What I would like to do is to handle the get request in Django, extract the uid and token, and then make a POST request. I have extracted the uid and token and would like to make a POST (within this GET request). I do not know how to make this POST request in the background.

My URL is like this:

http://127.0.0.1:8000/auth/users/activate/MQ/4qu-584cc6772dd62a3757ee

When I click on this in an email it does a GET request.

I handle this in a Django view.

The view needs to make a POST request like this:

http://127.0.0.1:8000/auth/users/activate/

data= [(‘uid’=‘MQ’), (‘token’=‘4qu-584cc6772dd62a3757ee’),]

My view to handle GET is:

from rest_framework.views import APIView
from rest_framework.response import Response
import os.path, urllib


class UserActivationView(APIView):
    
    def get (self, request):
        urlpathrelative=request.get_full_path()
        ABSOLUTE_ROOT= request.build_absolute_uri('/')[:-1].strip("/")

        spliturl=os.path.split(urlpathrelative)
        relpath=os.path.split(spliturl[0])
        uid=spliturl[0]
        uid=os.path.split(uid)[1]
        
        token=spliturl[1]
        postpath=ABSOLUTE_ROOT+relpath[0]+'/'
        post_data = [('uid', uid), ('token', token),]     
        result = urllib.request.urlopen(postpath, urllib.parse.urlencode(post_data).encode("utf-8"))
        content = result.read()
        return Response(content)
leferaycloud
  • 83
  • 1
  • 5

1 Answers1

13

views.py

from rest_framework.views import APIView
from rest_framework.response import Response
import requests

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)

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^auth/users/activate/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', UserActivationView.as_view()),
]
Samet Genc
  • 251
  • 2
  • 4
  • How will we set the email smpt etc is there any and to end code available . I read the documentation but cant set it up – Sourav Roy Jan 20 '20 at 16:10
  • @SouravRoy using gmail, the following link shows the required settings. Note that you also have to change a setting in Gmail itself (just google "Enabling less secure apps to access Gmail" and you will find out how), link to example settings.py: https://stackoverflow.com/questions/59335152/accounts-activation-with-djoser-and-django-rest-framework – Rik Schoonbeek Mar 13 '20 at 20:32
  • Instead of returning text as response, it would be better if we return the json and statuscode `return Response(result.json(), status=result.status_code) ` – Darsh Patel May 06 '20 at 13:05
  • 3
    typo: `result.text()` should be `result.text` – digitalfoo May 30 '20 at 04:43
  • 2
    This hangs my debug django server – Hack_Hut Apr 25 '21 at 11:55
  • 1
    @Hack_Hut my server also hangs.. can you suggest some way to resolve this? I have asked a question regarding this: [Requests to localhost from within a view cause the server to stop responding](https://stackoverflow.com/questions/68401321/requests-to-localhost-from-within-a-view-cause-the-server-to-stop-responding) – Sabito stands with Ukraine Jul 15 '21 at 22:43