模板html
建立项目前端
初始化项目python
django-admin startproject tmpl cd tmpl python manage.py startapp learn
修改tmpl/settings.pydjango
ALLOWED_HOSTS = [ '虚拟机ip地址', 'www.mysite.com' ] INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'learn', )
在learn目录下新建templates文件夹session
cd templates touch index.html
index.html内容以下:app
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>TMPL Index</title> </head> <body> <div>TMPL Index</div> </body> </html>
修改learn/views.py
修改以下:url
from django.shortcuts import render def home(request): return render(request, 'index.html')
修改tmpl/urls.py
修改以下:code
from django.conf.urls import url from django.contrib import admin from learn import views as learn_views urlpatterns = [ url(r'$', learn_views.home, name='home'), # 新增 url(r'^admin/', admin.site.urls), ]
备注:若是有多个应用时,能够在templates文件夹下新建一个learn文件夹,把模板文件index.html移动到templates/learn/下,views.py中的home方法也须要做出相应的修改:server
def home(request): return render(request, 'learn/index.html')
这样作的目的是,让Django可以正确查找到home.htmlhtm
模板继承
在templates/learn新建base.html,内容以下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>{% block title %} 默认标题{% endblock %}</title> </head> <body> {% block content %} <div>这里是默认内容区</div> {% endblock %} </body> </html>
修改index.html, 修改以下:
{% extends 'learn/base.html' %} {% block title %} 首页 {% endblock %} {% block content %} <div> 这里是首页 </div> {% endblock %}
运行
python manage.py runserver 0.0.0.0:8000
主机访问:www.pyl.com:8000 ,输出:这里是首页
感受 写习惯了前端,以为模板继承写起来好麻烦,还要加{% %}和标记开头和结束,jade模板语法很好看,要是Python模板的语法也那样就行了