带有django的动态html表 [英] Dynamic html table with django
本文介绍了带有django的动态html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用从数据库的日期组成的表实现HTML页.而且,如果我向数据库添加元素,我也想对HTML表进行更新.如何用django实现它?
I want to realize HTML page with table which consist from date from database. And if I add element to database I want to HTML table updated too. How to realize it with django?
推荐答案
您可以编写如下内容:
在您的 models.py 文件中:
class MyModel(models.Model):
foo = models.CharField(max_length=...)
bar = models.CharField(max_length=...)
...
def __str__(self):
return self.foo, self.bar
然后,在您的 views.py 文件中:
def MyFunction(request):
my_var = MyModel.objects.all()
return render(request, 'Template.html', {"my_var": my_var})
最后在您的 template.html 文件中:
{% load staticfiles %}
{% load static %}
{% block content %}
{% for object in my_var_list %}
<table style="width:90%">
<tbody>
<p></p>
<tr>
<td>foo</td>
<td>{{ object.foo }}</td>
</tr>
<tr>
<td>bar</td>
<td>{{ object.bar }}</td>
</tr>
</tbody>
</table>
{% endfor %}
{% endblock content %}
下次,请阅读StackOverflow文档以及发布问题之前所做的工作.
Next time, read StackOverflow documentations, and what you have done before to post your question.
这篇关于带有django的动态html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文