特定用户

时间:2018-05-14 17:16:48

标签: django django-login

我正在尝试在我创建的链接上自动登录用户,因此我只是尝试为此启用特定的网址。

所以我有一个简单的登录视图:

from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect, render
from django.views.generic import View
from django.contrib.auth import login, authenticate

import logging

logger = logging.getLogger(__name__)


class SpecialUserGatewayView(View):
    def post(self, request):

        token = request.POST['token']
        user = authenticate(token=token)

        if user is not None:
            if user.is_specific_user:
                return HttpResponseRedirect('dashboard')
            else:
                return HttpResponse("This user is not Specific User!")
        else:
            return HttpResponseRedirect('/')

并且这个网址是

url(r'^special_user/login/(?P<token>[0-9A-Za-z])/$,', SpecialGatewayView.as_view(), name="special-login")

现在我使用rest framework jwt生成令牌,我的登录网址应该是这样的https://mywebpage/special_user/login/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImExQGExc3VwZXJ1c2VyLmNvbSIsImVtYWlsIjoiYTFAYTFzdXBlcnVzZXIuY29tIiwiZXhwIjoxNTI2MzE5OTk0LCJ1c2VyX2lkIjo1Miwib3JpZ19pYXQiOjE1MjYzMTY5OTR9.-pUBVjiAbRhgfuj5IFQP7Qh9KXRX4K_Tyn0nsucF1pM

错误是:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8888/special_user/login/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImExQGExc3VwZXJ1c2VyLmNvbSIsImVtYWlsIjoiYTFAYTFzdXBlcnVzZXIuY29tIiwiZXhwIjoxNTI2MzE5OTk0LCJ1c2VyX2lkIjo1Miwib3JpZ19pYXQiOjE1MjYzMTY5OTR9.-pUBVjiAbRhgfuj5IFQP7Qh9KXRX4K_Tyn0nsucF1pM/?next=/dashboard/

正如您所看到的,我没有向我的应用程序发送更好的网址,所以有人可以帮助我并解释我如何克服这一点,谢谢。

1 个答案:

答案 0 :(得分:1)

你有很多错误。首先,您尝试使用GET方法登录,但在视图中使用POST方法。其次,您不需要为GET方法创建URL,您可以将GET参数发送到任何视图。

要修复错误,请将网址更改为:

url(r'^special_user/login', SpecialGatewayView.as_view(), name="special-login")

你的观点应该是这样的:

class SpecialUserGatewayView(View):
    def get(self, request): # Change to GET

        token = request.GET['token'] # Change to GET
        user = authenticate(token=token)

        if user is not None:
            if user.is_specific_user:
                return HttpResponseRedirect('dashboard')
            else:
                return HttpResponse("This user is not Specific User!")
        else:
            return HttpResponseRedirect('/')