python如何在前端显示后端的信息

首先在test.html中需要添加如下:

1
2
3
4
5
6
7
8
<html>
<body>
<h1>下面是后端返回的内容</h1>
 
{{ xianshi }}
 
</body>
</html>



后端代码:

1
2
3
4
5
import datetime
from django.shortcuts import render_to_response
def current(request):
    now=datetime.datetime.now()
    return render_to_response("test.html",{'xianshi':now})


在前端html中循环后端的代码:

1
2
3
4
5
6
7
8
9
10
<html>
<body>
<h1>下面是后端返回的内容</h1>
 
{% for i in xianshi %}
{{ i }}
{% endfor %}
 
</body>
</html>

再加上if语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<body>
<h1>下面是后端返回的内容</h1>
 
{% for i in xianshi %}
  {% if "2" in i %}
      <p style="color:red">{{ i }}</p>
  {% else %}
      <p style="color:green">{{ i }}</p>
   {% endif %}
{% endfor %}
 
</body>
</html>



如果代码量太大,排错的话可以装个django-debugtools

1
pip install django-debugtools


上一篇:mysql hash 索引 vs B-TREE 索引 理解


下一篇:PolarDB-X 1.0-API参考-1.0(2019版本)-其他-DescribeInstanceSwitchAzone