[关闭]
@breakerthb 2016-10-31T02:51:09.000000Z 字数 2834 阅读 1563

Django模板

Django


创建项目和应用

创建一个mysite项目,和一个 名称为learn的应用,并且

$ django-admin.py startproject mysite
	$ cd mysite
$ python manage.py startapp learn

把 learn 加入到 settings.INSTALLED_APPS中

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'learn',
)

首页

打开 learn/views.py 写一个首页的视图

from django.shortcuts import render

def home(request):
    return render(request, 'home.html')

模板文件夹

在 learn目录下新建一个 templates 文件夹,里面新建一个home.html

默认配置下,Django 的模板系统会自动找到app下面的templates文件夹中的模板文件。

在 home.html 中写一些内容

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
Hello World
</body>
</html>

添加映射关系

将视图函数对应到网址,更改 mysite/urls.py

Django 1.7.x 及以下可以这样:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^$', 'learn.views.home', name='home'),  # new
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
]

Django 1.8.x 及以上:

from django.conf.urls import include, url
from django.contrib import admin
from learn import views as learn_views

urlpatterns = [
    url(r'^$', learn_views.home, name='home'),
    url(r'^admin/', include(admin.site.urls)),
]

创建数据库表(可选)

$ python manage.py syncdb

Django 1.9.x 以及上要用

$ python manage.py migrate

看效果

$ python manage.py runserver

模板常用技巧

网站模板的设计,一般的,我们做网站有一些通用的部分

模板引入

可以写一个 base.html 来包含这些通用文件(include)

<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
{% load staticfiles %}
<link rel=stylesheet type="text/css" href="{% static "style/default.css" %}" title="cool">
</head>

<body background="{% static "image/bg.jpg"%}">

{% include 'nav.html' %}

<h1>
{% block pagename %}
Directionary
{% endblock %}
</h1>
<hr/>

{% block content %}
<div>这里是默认内容,所有继承自这个模板的,如果不覆盖就显示这里的默认内容。</div>
{% endblock %}

{% include 'bottom.html' %}

{% include 'tongji.html' %}
</body>
</html>

模板继承

在其他文件中对base.html文件进行继承:

{% extends 'base.html' %}

{% block title %}欢迎光临首页{% endblock %}

{% block content %}
{% include 'ad.html' %}
这里是首页,欢迎光临
{% endblock %}

Django 模板查找机制

Django 查找模板的过程是在每个 app 的 templates 文件夹中找(而不只是当前 app 中的代码只在当前的 app 的 templates 文件夹中找)。各个 app 的 templates 形成一个文件夹列表,Django 遍历这个列表,一个个文件夹进行查找,当在某一个文件夹找到的时候就停止,所有的都遍历完了还找不到指定的模板的时候就是 Template Not Found (过程类似于Python找包)。这样设计有利当然也有弊,有利是的地方是一个app可以用另一个app的模板文件,弊是有可能会找错了。所以我们使用的时候在 templates 中建立一个 app 同名的文件夹,这样就好了。

这就需要把每个app中的 templates 文件夹中再建一个 app 的名称,仅和该app相关的模板放在 app/templates/app/ 目录下面,

例如:项目 zqxt 有两个 app,分别为 tutorial 和 tryit

zqxt
├── tutorial
│   ├── __init__.py
│   ├── admin.py
│   ├── models.py
│   ├── templates
│   │   └── tutorial
│   │       ├── index.html
│   │       └── search.html
│   ├── tests.py
│   └── views.py
├── tryit
│   ├── __init__.py
│   ├── admin.py
│   ├── models.py
│   ├── templates
│   │   └── tryit
│   │       ├── index.html
│   │       └── poll.html
│   ├── tests.py
│   └── views.py
├── manage.py
└── zqxt
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

这样,使用的时候,模板就是 "tutorial/index.html" 和 "tryit/index.html" 这样有app作为名称的一部分,就不会混淆。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注