python - 使用 Django 显示表格中的数据

标签 python django

<分区>

谁能给我一小段代码,我们可以在其中显示表格中的数据,并指出进入 views.py 的代码和 templates/index.html显示目录?

最佳答案

Read the Django tutorial .它分 4 个部分展示了该框架的基础知识。

至于你的问题,一个非常简单的例子。

在 views.py 中:

def display(request):
  return render_to_response('template.tmpl', {'obj': models.Book.objects.all()})

在 models.py 中:

class Book(models.Model):
  author = models.CharField(max_length = 20)
  title = models.CharField(max_length = 40)
  publication_year = models.IntegerField()

在 template.tmpl 中:

<table>
<tr>
  <th>author</th>
  <th>title</th>
  <th>publication year</th>
</tr>
{% for b in obj %}
<tr>
  <td>{{ b.author }}</td>
  <td>{{ b.title }}</td>
  <td>{{ b.publication_year }}</td>
</tr>
{% endfor %}
</table>

关于python - 使用 Django 显示表格中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1865479/

相关文章:

python - 用于 visual studio 的 python 工具中的 Intellisense

python - django 验证器和 clean_field 方法之间的区别

python - 我如何在 Django 中定义 3 个以上模型之间的多对多关系?

python - 在 Python 2.7 中表示 µs

python - 如何在 Tensorflow 中计算整个数据集的统计数据(总和、均值、方差等)

python - TensorFlow : Tried to convert 'input' to a tensor and failed. 错误:不支持无值

python - Django 如何在 Windows 10 中使用 redis?

python - 前向声明 - django 中没有管理页面?

Django:以 10 为底的 int() 的无效文字

python - 如何将日期选择器放入 django 应用程序中?