$ cd learn $ mkdir templates && cd templates $ vim home.html
<!DOCTYPE html> <html> <head> <title>shiyanlou</title> </head> <body> Programming every time, everywhere. </body> </html>
老规矩,在修改完 views.py 之后,来配置一下链接路由:
1 2 3 4 5 6 7 8 9 10 11 12 13
$ vim dj4/urls.py from django.conf.urls import include, url from django.contrib import admin from learn import views as learn_views from calc import views as calc_views
urlpatterns = [ url(r'^admin/', include(admin.site.urls)), # url(r'$', learn_views.index), # url(r'^add/', calc_views.add, name = 'add'), # url(r'^add/(\d+)/(\d+)/$', calc_views.add2, name = 'add2'), url(r'^$', learn_views.home, name = 'home'), ]
接下来测试查看效果:
发现我们的 html 模板文件成功加载了。这样相对于只有输出日志的文字流灵活更多了。但是,Django 的模板文件不仅仅停留在 html 加载,而是一种新的标记语言。
深入模板学习
在很多时候,我们所做的网站有很多通用的部分。例如导航条、页底、访问统计等等。我们可以使用 include 关键字,来通过引入通用文件来插入 html 标记中:
$ vim learn/templates/home.html <!DOCTYPE html> <html> <title>shiyanlou</title> <body> Programming every time, everywhere. There are some courses: <br /> {% for i in TutorialList %} {{ i }} {% if not forloop.last %} , {% endif %} {% endfor %}