Django Python::如何在Render/Redi中将变量传递给另一个页面

2024-09-30 16:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个登录页面a,在这个页面中,我获取User/Pwd信息,然后发布到另一个第三方网站,得到JSON格式的sessionid变量。当我重定向/呈现摄取页面B时,我需要这个sessionid变量在那里显示/可用。在

这是我试过的,但我似乎不能使它发挥作用。有人能帮忙吗?在

在网址.py在

from django.urls import path
from loginp.views import LoginView
from loginp.views import IngestionView

urlpatterns = [
    path('login/', LoginView.as_view(),name='LoginView'),
    path('ingestion/', IngestionView.as_view(template_name='registration/ingestion.html'),name='IngestionView'),]

在视图.py在

^{pr2}$

在登录.html在

<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<div class="container">
        <div class="login-form col-md-4 offset-md-4">
            <h4 class="title">Please Login</h4>
            <br>
            <form action="{% url 'IngestionView' %}" method="POST">
            <div class="form-group" >
              {% csrf_token %}  
              {{form.as_p}}     
              <br>
              <button class="btn btn-primary btn-block" type="submit" value='submit'>login</button>
             </div>
            </form>
        </div>
</div>
</body>
</html>

在摄取.html在

<html>
<head>
<title>Ingestion Details</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<div class="container">
            <h4 class="title">Ingestion Details</h4>
            <br>
            <form method="POST">    
            <div class="form-group" >
              {% csrf_token %}  
              {{ form.as_p }}       
              <br>
              <button class="btn btn-primary btn-block" type="submit" value='submit'>Do something</button>

             </div>
            </form>
        </div>
</div>
<h2>Session Id {{ sessionid }}</h2>
</body>
</html>

<;----新代码------>; 视图.py在

data_json = json.dumps(data)
headers = {'Content-type': 'application/json'}
url='https://dm-us.informaticacloud.com/ma/api/v2/user/login'
response = requests.post(url, data=data_json, headers=headers)
sessionid=response.json()['icSessionId']
request.session['sessionid'] = sessionid
request.session.save()
form1 = IngestionForm()
return render(request,'registration/ingestion.html',{'sessionid':sessionid}) #{'info':args})

在摄取.html在

<h2>Session Id {{ request.session.get('sessionid') }}</h2>

Tags: divformjsontitlehtmlasloginbootstrap
1条回答
网友
1楼 · 发布于 2024-09-30 16:33:58

您可以将此值保存到Django的session。在第一个视图中:

sessionid=response.json()['icSessionId']
request.session['sessionid'] = sessionid

在第二个视图中:

^{pr2}$

相关问题 更多 >