FBV(function base view),即一个url对应views.py一个函数
示例演示如下
1)FBV如何使用
2)渲染页面,并返回字典数据
3)字典数据页面如何访问
1)url.py
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^login/', views.login),
url(r'^home/', views.Home.as_view()),
url(r'^detail-(\d+).html',views.detail), #动太路由
]
2)views.py
USER_DICT={
"1":{"name":"root","email":"[email protected]" },
"2":{"name":"root","email":"[email protected]" },
"3":{"name":"root","email":"[email protected]" },
"4":{"name":"root","email":"[email protected]" }, }
def index(request): return render(request,"index.html",{"USER_DICT":USER_DICT})
3)模板index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
{% for row in USER_DICT %} <!-- 这里返回的是KEY-->
<li>{{ row }}</li>
{% endfor %} {% for row in USER_DICT.keys %} <!-- 这里返回的是KEY-->
<li>{{ row }}</li>
{% endfor %}
{% for row in USER_DICT.values %} <!-- 这里返回的是values-->
<li><a href="/detail/?id={{ row }}">{{ row }}</a></li>
{% endfor %}
{% for k,v in USER_DICT.items %} <!-- 这里返回的是key,values-->
<li><a href="/detail-{{ k }}.html">{{ v.name }}</a></li>
{% endfor %}
</ul>
</body>
</html>