Django-Python ::如何在渲染/重定向上将变量传递到另一个页面

时间:2018-08-06 04:30:51

标签: python django variables redirect response

我有一个登录页面A,在其中我获取用户/密码信息,并发布到另一个第三方网站,并获取JSON格式的sessionid变量。当我重定向/呈现提取页面B时,我需要在该会话ID变量中显示/提供该变量。

这是我尝试过的方法,但似乎无法使其正常工作。有人可以帮忙吗?

urls.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'),]

views.py

from django.views.generic import TemplateView
from django import forms
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from .forms import LoginForm
from .forms import IngestionForm
import json
import requests

class LoginView(TemplateView):
template_name = 'registration/login.html'

def get(self, request):
    form = LoginForm()
    return render(request, self.template_name, {'form':form})

def post(self, request):
    form = LoginForm(request.POST)
    if form.is_valid():
        #print form.cleaned_data()
        text = form.cleaned_data['email']
        pwd = form.cleaned_data['password']

    data = {
     "@type": "login",
     "username": text,
     "password": pwd
    }


    data_json = json.dumps(data)
    headers = {'Content-type': 'application/json'}
    url='https://cloud.com/ma/api/v2/user/login'
    response = requests.post(url, data=data_json, headers=headers)
    sessionid=response.json()['icSessionId']

    form1 = IngestionForm()
    #return render(request,'registration/ingestion.html',{'sessionid':sessionid}) #{'info':args})
    #return HttpResponseRedirect(reverse('IngestionView', kwargs={'sessionid':sessionid}))
    return redirect(request,'registration/ingestion.html',sessionid=sessionid) 


class IngestionView(TemplateView):
template_name = 'registration/ingestion.html'


def post(self, request):
    form1 = IngestionForm(request.POST)

    args1 = {"form": form1}
    return render(request, self.template_name, args1)

login.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>

ingestion.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>

<-----新代码-----> views.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})

ingestion.html

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

1 个答案:

答案 0 :(得分:2)

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

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

在第二个视图中:

class IngestionView(TemplateView):
    template_name = 'registration/ingestion.html'


    def post(self, request):
        form1 = IngestionForm(request.POST)
        sessionid=request.session['sessionid']
        args1 = {"form": form1, 'sessionid': sessionid}
        return render(request, self.template_name, args1)