{% for item in data %} <tr role="row" class="odd"> <td>{{ item.id }}</td> <td>{{ item.pub_date }}</td> <td><img src="/media/{{ item.file }}"/></td> <td>{{ item.category }}</td> <td><a href="/delete/{{ item.id }}">删除</a></td>
时间: 2023-12-30 10:06:27 浏览: 20
这是一个 Django 模板语言的 for 循环,用于渲染一个数据表格。它遍历一个名为 data 的列表,将其中的每个元素作为 item 变量传递到循环体中。循环体中使用 item.id、item.pub_date、item.file 和 item.category 依次访问每个元素的属性,并将它们渲染到 HTML 表格中的对应单元格中。最后,循环体中还包括一个删除链接,用于删除当前元素。
相关问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户</title> <link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/4.5.3/css/bootstrap.css}"> <style type="text/css"> .odd{ background-color: antiquewhite; } .even{ background-color: aquamarine; } </style> <script type="text/javascript" th:src="@{/webjars/jquery/3.5.1/jquery.js}"></script> <script type="text/javascript" th:src="@{/webjars/bootstrap/4.5.3/js/bootstrap.min.js}"></script> </head> <body> <table border="3" class="table"> <tr> <th>序号</th><th>用户名</th><th>角色</th> </tr> <tr th:each="user:${users.list}"> <td th:text="${user.userId}">0</td> <td th:text="${user.userName}">地球</td> <td th:text="${user.roleName}">admin</td> </tr> </table> </body> </html>美化这个页面,使表格占满页面
可以尝试以下样式代码来美化页面,使表格占满页面:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户</title>
<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/4.5.3/css/bootstrap.css}">
<style type="text/css">
.odd{
background-color: antiquewhite;
}
.even{
background-color: aquamarine;
}
body{
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.table{
width: 100%;
}
</style>
<script type="text/javascript" th:src="@{/webjars/jquery/3.5.1/jquery.js}"></script>
<script type="text/javascript" th:src="@{/webjars/bootstrap/4.5.3/js/bootstrap.min.js}"></script>
</head>
<body>
<table border="3" class="table">
<tr>
<th>序号</th><th>用户名</th><th>角色</th>
</tr>
<tr th:each="user:${users.list}">
<td th:text="${user.userId}">0</td>
<td th:text="${user.userName}">地球</td>
<td th:text="${user.roleName}">admin</td>
</tr>
</table>
</body>
</html>
```
这里使用了 flex 布局,将页面垂直居中,同时让表格宽度为 100% ,占满整个页面。
<tr class="part odd">怎么提取出class的text
你可以使用 BeautifulSoup 库来提取 HTML 标签中的 class 属性的文本内容。首先,你需要使用 BeautifulSoup 解析 HTML 文档。然后,使用 `find_all` 方法找到所有包含指定 class 的标签,并使用 `text` 属性获取它们的文本内容。
下面是一个示例代码:
```python
from bs4 import BeautifulSoup
# 假设你有一个 HTML 字符串
html = '''
<html>
<body>
<div class="part odd">这是第一个标签</div>
<div class="part even">这是第二个标签</div>
</body>
</html>
'''
# 使用 BeautifulSoup 解析 HTML
soup = BeautifulSoup(html, 'html.parser')
# 找到所有包含指定 class 的标签
tags = soup.find_all(class_="part odd")
# 输出文本内容
for tag in tags:
print(tag.text)
```
运行这段代码,你将得到输出:`这是第一个标签`。你可以将 `class_` 参数设置为你想提取的 class 名称,然后使用循环遍历找到的标签并获取其文本内容。