STATIC_PATH = os.path.join(BASE_DIR,'static')STATIC_URL = '/static/' # You may find this is already defined as such.STATICFILES_DIRS = ( STATIC_PATH,)模板文件路径TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [TEMPLATE_PATH], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, },]mysite工程中的urls.py中增加内容 url(r'^app1/', include('app1.urls')),app1中新增的urls.py中内容from django.conf.urls import patterns, urlfrom app1 import viewsurlpatterns = [ url(r'^$', views.index, name='index')]为了建立映射,我们用到了tuple.在Django里必须用urlpatterns来命名这个元组.这个urlpatterns元组包含一些django.conf.urls.url()函数的调用,而每个函数里都有一个唯一的映射.在上面的代码里,我们只用了url()一次,所以我们只映射了一个URL.django.conf.urls.url()函数的第一个参数是正则表达式^$,指的是匹配一个空字符串.所有匹配这个模式的URL都会映射到views.index()这个视图.用户的请求信息会包含在HttpRequest对象里作为参数传递给视图.我们给url()函数可选参数name赋值为index.index.html内容如下<!DOCTYPE html>{% load staticfiles %} <!-- New line --><html> <head> <title>SSOC</title> </head> <body> <h1>SSOC says...</h1> hello world! <strong>{{ boldmessage }}</strong><br /> <img src="{% static "images/lilies.jpg" %}" alt="Picture of app1" /> </body></html></html>首先,我们需要使用{% load static %}标签来使用静态媒体.所以我们才可以用{% static "rango.jpg" %在模板里调用static文件.Django模板标签用{ }来表示.在这个例子里我们用static标签,它将会把STATIC_URL和rango.jpg连接起来,如下所示.<img src="/static/images/rango.jpg" alt="Picture of Rango" /> <!-- New line -->如果因为什么原因图片不能加载我们可以用一些文本来代替.这就是alt属性的作用 - 如果图片加载失败就显示alt属性中的文本.好了,让我们再次运行Django服务访问http://127.0.0.1:8000/rango.幸运的话可以看到下图.模板========================mysite├── app1│ ├── __init__.py│ ├── admin.py│ ├── migrations│ │ └── __init__.py│ ├── models.py│ ├── tests.py│ ├── urls.py │ ├── apps.py│ └── views.py├── templates │ ├── base.xml 新增│ ├── nav.xml 新增│ └── app1│ └── index.html新增├── static │ ├── images │ └── bootstrap ├── manage.py└── mysite ├── __init__.py ├── settings.py ├── urls.py └── wsgi.pyindex.xml{% extends 'base.html' %} {% block title %}My-SSOC{% endblock %} {% block content %} hello world! <p class="text-warning">kkkkkkkkkkkk warning class</p><p class="text-danger">fffffffff danger class</p>{% endblock %}base.html<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>{% block title %}SSOC{% endblock %} - wangyu</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/CSS/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> {% include 'nav.html' %} {% block content %} <div>This is origin content</div> {% endblock %} </body></html>nav.html<nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">SSOC</a> </div> <div> <ul class="nav navbar-nav"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">haha<b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="#">a1</a></li> <li><a href="#">a22</a></li> <li><a href="#">Jaf Report</a></li> <li class="divider"></li> <li><a href="#">df</a></li> <li class="divider"></li> <li><a href="#">bg</a></li> </ul> </li> <li><a href="#">SVN</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">java<b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="#">jmeter</a></li> <li><a href="#">EJB</a></li> <li><a href="#">Jasper Report</a></li> <li class="divider"></li> <li><a href="#">divide</a></li> <li class="divider"></li> <li><a href="#">another</a></li> </ul> </li> </ul> </div> </div></nav>
新闻热点
疑难解答