首先在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
|
本文转自 王家东哥 51CTO博客,原文链接:http://blog.51cto.com/xiaodongge/1903511