在Django中使用数据重定向



我有一个通过链接的用户授权:

def ClientAuth(request, link_code):
try:
code = Links.objects.filter(code=link_code).values('code', 'status')
if code[0]['status']:
username, password = 'Client', '**************'
client = authenticate(request, username=username, password=password)
if client is not None:
login(request, client)
return redirect('clientPage')
return HttpResponse("Hello its work")
else:
return render(request, 'client/404.html')
except:
return render(request,'client/404.html')

如果链接存在于数据库中,并且活动的一个授权我们作为客户端并将其重定向到他的页面,它看起来像url:

urlpatterns = [
path('clientPage/',views.clientPage, name = 'clientPage'),
path('<link_code>/', views.ClientAuth, name='ClienAuth')
]

我的任务是,根据客户端登录时使用的链接,他会收到不同的信息,例如,链接ID

重定向时,是否有任何方法可以从ClientAuth函数传递此数据?

重定向到本页:

def clientPage(request):
print(request)
return HttpResponse("Hello")

您必须按如下方式更改url:

urlpatterns = [
path('clientPage/<link_code>',views.clientPage, name = 'clientPage'),
path('<link_code>/', views.ClientAuth, name='ClienAuth')
]

然后更改视图,如下所示:

def ClientAuth(request, link_code):
try:
code = Links.objects.filter(code=link_code).values('code', 'status')
if code[0]['status']:
username, password = 'Client', '**************'
client = authenticate(request, username=username, password=password)
if client is not None:
login(request, client)
return redirect('clientPage', link_code=link_code)
return HttpResponse("Hello its work")
else:
return render(request, 'client/404.html')
except:
return render(request,'client/404.html')

def clientPage(request, link_code=''):
print(request)
context = {'link_code': link_code}
return render(request, 'test_app/index.html', context)

最新更新