程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The front end (HTML) can receive the list data of the back end (Python view.py) render. How can the front end display a value of the list in a grid through a table?

編輯:Python
The phenomenon and background of the problem

use Excel Make website database .python The library used is Django、xlrd、xlrw、xlutils.
Excel Read normal , You can extract table data into lists , You can pass the list data through

render The way in view.py To the corresponding html In the web page . The web page also received a list .
Want to pass table stay html Display the contents of the list on the , The grid of a table corresponds to a list value . But somehow after the output, the list value is split into a single character . What is the solution . Specifically, what went wrong .

view.py Render function returned from file ,ownerlist[0][1] It's a list , Pass it on to bms_work.html:
return render(request, 'bms_work.html',
{'company': company, 'name': name, 'ownerlist': ownerlist[0][1], 'sh_rnlist': ownerlist[1]})

bms_work.html The tag statements in are as follows :

<tr> <td width="300px"> {% for item1 in ownerlist %} {
{ item1 }} </td></tr>{% endfor %}
###### Operation results and error reporting contents

My solution ideas and tried methods
What I want to achieve

One grid, one list value




Take the answer :

you ownerlist[0][1] Is a string , Use eval(ownerlist[0][1]) Convert to list

Do you use Excel Make website database ,Excel Only numeric values or strings can be stored in a cell , When you store the list in a cell, you turn the list into a string
you view.py Used in documents
print(type(ownerlist[0][1]))
See if it is a string str object
If yes render() Medium to 'ownerlist': eval(ownerlist[0][1])

return render(request, 'bms_work.html',{
'company': company, 'name': name, 'ownerlist': eval(ownerlist[0][1]), 'sh_rnlist': ownerlist[1]})


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved