首页 > 解决方案 > 来自views.py的值,没有反映在HTML页面中:Django

问题描述

我在将元素从 views.py 传递到 html 页面时遇到问题,首先我将解释 UI,我有一个带有名称的 html 页面。

side-menu-light-tabulator.html

和 UIlooks 是这样的 用户界面1

当我点击显示服务器列表时,它将显示 div 并向我显示另一个 div,其中另一个表替换旧表,因为您可以看到“文档编辑器”在仅标题表更改中保持不变。

UI2

将第一个 UI 视为表 1,将第二个 UI 视为表 2,因此我通过对表 1 数据执行一些查询来获取表 2 的数据,表 1 工作正常,并且在使用从表 1 获得的数据执行查询时也打印在终端,但是当我通过返回渲染发送数据时,它没有打印在表 2 中。

所以这是我的代码:

def secondtableonDashboard(request):
    Server_list = ""
    NewList = ""
    afterConcat = ""
    conn = pyodbc.connect('Driver={SQL Server};'
                          'Server=ABC\SQLEXPRESS;'
                          'Database=WebstartUI;'
                          'Trusted_Connection=yes;')

    cursor = conn.cursor()
    cursor.execute("select * from CustomerServer")
    result = cursor.fetchall()

    if request.method=='POST': # ------> getting value from first table 
       user_list = request.POST.getlist('checked_list')
       Server_list = listToString(user_list) +","
    else:
       print("No Values")

    for listValue in Server_list.split(","):
        NewList = "'"+listValue+"',"
        afterConcat = afterConcat + NewList
    Server_list = afterConcat[:-5]
    Server_list = Server_list[1:]
    ServiceList = conn.cursor()
    cmd = "Select * from dbo.ServiceRunningStatus where ServerName IN ('"+Server_list+"')" #---> performing query 
    ServiceList.execute(cmd)
    resultOfServiceList = ServiceList.fetchall()
    print(resultOfServiceList) #---> result gets printed perfectly .

    return render(request,'side-menu-light-tabulator.html',{'customerserver':result,'ServerList':resultOfServiceList}) #--> 

返回结果...这里 customerserver 是第一个表数据,ServerList 是第二个表数据

html:

 {% for services in ServerList %}

  <td>{{services.ServerName}}</td>
  <td>{{services.Component}}</td>
  <td>{{services.PID}}</td>
   <td>{{services.State}}</td>
  {% endfor %}

但此数据未打印在第二张表上,也未打印在网络响应预览中。谁能帮我找出我做的错误

图片

标签: djangorender

解决方案


推荐阅读