Djoser用户激活电子邮件POST示例 [英] Djoser user activation email POST example

查看:106
本文介绍了Djoser用户激活电子邮件POST示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DREF和Djoser进行身份验证和用户注册。
新用户注册后,Djoser会发送一封激活电子邮件,其中包含执行GET请求的链接。为了激活,我需要从激活URL中提取uid和token,并向POST请求Djoser激活用户。

I am using DREF 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 uid and token from the activation url and make a POST request for Djoser to be able to activate the user.

我的环境是Python 3和Django 1.11,Djoser 1.0.1。

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

在Django / Djoser中如何处理此问题有帮助吗?

Any help on how to handle this in Django / Djoser?

我想做的是在Django中处理get请求,提取uid和token,然后发出POST请求。我已经提取了uid和token,并希望进行POST(在此GET请求中)。
我不知道如何在后台发出此POST请求。

What I would like to do is to handle the get request in Django, extract uid and token and then make a POST request. I have extracted 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.

添加代码:

我的网址是这样的: http://127.0.0.1:8000/auth/users/activate/MQ/4qu-584cc6772dd62a3757ee

当我单击它时(在电子邮件中它发出GET请求。

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

我在Django视图中处理此问题。

I handle this in a Django view.

该视图需要发出这样的POST请求:

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'),]

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

我要处理的视图GET是:

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)


推荐答案

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()),
]

这篇关于Djoser用户激活电子邮件POST示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆