我正在尝试在我创建的链接上自动登录用户,所以我尝试只为它启用一个特定的url网关。
所以我有一个简单的登录视图:
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框架jwt生成令牌,我的登录url应该类似于这个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/正如你所看到的,我没有发送好的url到我的申请和更多,所以谁能帮助我,并解释我如何克服这一点,谢谢。
发布于 2018-05-14 17:42:50
你有很多错误。首先,您尝试使用GET方法登录,但在视图中使用POST方法。其次,您不需要为GET方法创建一个url,您可以将GET参数发送到任何视图。
若要修复错误,请将url更改为:
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('/')https://stackoverflow.com/questions/50335740
复制相似问题