使用Django中的数据重定向

krcsximq  于 2022-12-24  发布在  Go
关注(0)|答案(1)|浏览(152)

我通过链接获得了用户授权:

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")
yjghlzjz

yjghlzjz1#

您必须更改网址如下:

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)

相关问题