我正在使用DREF和Djoser进行身份验证和用户注册。当新用户注册时,Djoser将发送激活电子邮件,其中包含执行GET请求的链接。为了激活,我需要从激活URL中提取uid和token并向POST请求Djoser激活用户。
我的环境是Python 3和Django 1.11,Djoser 1.0.1。
有关如何在Django / Djoser中处理此问题的任何帮助?
我想做的是在Django中处理get请求,提取uid和token,然后发出POST请求。我已经提取了uid和token,并希望进行POST(在此GET请求中)。我不知道如何在后台发出此POST请求。
附加代码:
我的网址是这样的:http : //127.0.0.1 : 8000/auth/users/activate/MQ/4qu-584cc6772dd62a3757ee
当我单击此按钮(通过电子邮件发送GET请求)时。
我在Django视图中处理此问题。
该视图需要发出这样的POST请求:
http://127.0.0.1:8000/auth/users/activate/
数据= [(’uid’=’MQ’),(’token’=‘4qu-584cc6772dd62a3757ee’),]
我对GET的看法是:
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)
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()), ]