Django 1.10中文文档:第一个应用 part 6

已经同步到gitbook,想阅读的请转到gitbook: Django 1.10 中文文档

Writing your first Django app, part 6

This tutorial begins where Tutorial 5 left off. We’ve built a tested Web-poll application, and we’ll now add a stylesheet and an image.

紧接着教程5,我们已经创建了一个已被测试过的web投票应用,并且我们也加了样式和图片

Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, we refer to these files as “static files”.

除了由服务器生成的HTML文件外,网页应用一般需要提供其它必要的文件 —— 比如图片、JavaScript和CSS样式表 —— 来为用户呈现出一个完整的页面。 在Django中,我们将这些文件称为“静态文件”。

For small projects, this isn’t a big deal, because you can just keep the static files somewhere your web server can find it. However, in bigger projects – especially those comprised of multiple apps – dealing with the multiple sets of static files provided by each application starts to get tricky.

对于小型项目,这不是个大问题,将它们放在你的web服务器可以访问到的地方就可以了。 然而,在大一点的项目中 —— 尤其是那些由多个应用组成的项目 —— 处理每个应用提供的多个静态文件集合开始变得很难。

That’s what django.contrib.staticfiles is for: it collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.

这正是django.contrib.staticfiles要解决的:它收集每个应用(和任何你指定的地方)的静态文件到一个单独的位置,这个位置在线上可以很容易维护。

Customize your app’s look and feel

First, create a directory called static in your polls directory. Django will look for static files there, similarly to how Django finds templates inside polls/templates/.

首先在你的polls中创建一个static目录。Django将在那里查找静态文件,这与Django在polls/templates/中寻找对应的模板文件的方式是一样的

Django’s STATICFILES_FINDERS setting contains a list of finders that know how to discover static files from various sources. One of the defaults is AppDirectoriesFinder which looks for a “static” subdirectory in each of the INSTALLED_APPS, like the one in polls we just created. The admin site uses the same directory structure for its static files.

Django 的 INSTALLED_APPS设置包含一个查找器,它们知道如何从各种源找到静态文件。 其中默认的一个是AppDirectoriesFinder,它在每个INSTALLED_APPS下查找“static”子目录,就像刚刚在polls
中创建的一样。管理站点也为它的静态文件使用相同的目录结构。

Within the static directory you have just created, create another directory called polls and within that create a file called style.css. In other words, your stylesheet should be at polls/static/polls/style.css. Because of how the AppDirectoriesFinder staticfile finder works, you can refer to this static file in Django simply as polls/style.css, similar to how you reference the path for templates

在你刚刚创建的static目录中,创建另外一个目录polls并在它下面创建一个文件style.css。换句话讲,你的样式表应该位于polls/static/polls/style.css。根据AppDirectoriesFinder 静态文件查找器的原理,你可以通过polls/style.css在Django中访问这个静态文件,与你如何访问模板的路径类似。

Static file namespacing

Just like templates, we might be able to get away with putting our static files directly in polls/static (rather than creating another polls subdirectory), but it would actually be a bad idea. Django will choose the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

与模板类似,我们可以将静态文件直接放在polls/static(而不是创建另外一个polls 子目录),但实际上这是一个坏主意。Django将使用它所找到的第一个符合要求的静态文件的文件名,如果在你的不同应用中存在两个同名的静态文件,Django将无法区分它们。我们需要告诉Django该使用其中的哪一个,最简单的方法就是为它们添加命名空间。 也就是说,将这些静态文件放进以它们所在的应用的名字命名的另外一个目录下。

Put the following code in that stylesheet (polls/static/polls/style.css):

将以下代码写到样式表(polls/static/polls/style.css):

polls/static/polls/style.css

li a {
    color: green;
}

Next, add the following at the top of polls/templates/polls/index.html:

接下来在polls/templates/polls/index.html文件添加如下代码:

polls/templates/polls/index.html
{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

The{% raw %} {% static %}{% endraw %} template tag generates the absolute URL of static files.

{% raw %} {% static %}{% endraw %} 标签生成静态文件的绝对URL路径

That’s all you need to do for development. Reload http://localhost:8000/polls/ and you should see that the question links are green (Django style!) which means that your stylesheet was properly loaded.

这就是你在开发过程中所需要对静态文件做的所有处理。 重新加载http://localhost:8000/polls/,你应该会看到Question的超链接变成了绿色(Django的风格!),这意味着你的样式表被成功导入。

Adding a background-image

添加背景图片

Next, we’ll create a subdirectory for images. Create an images
subdirectory in the polls/static/polls/ directory. Inside this directory, put an image called background.gif. In other words, put your image inpolls/static/polls/images/background.gif.

下一步,我们将创建一个子目录来存放图片。 在polls/static/polls/目录中创建一个 images 子目录。在这个目录中,放入一张图片background.gif。换句话,将你的图片放在 polls/static/polls/images/background.gif。

Then, add to your stylesheet (polls/static/polls/style.css):

然后,向你的样式表添加(polls/static/polls/style.css):

polls/static/polls/style.css

body { background: white url("images/background.gif") no-repeat right bottom;}

Reload http://localhost:8000/polls/ and you should see the background loaded in the bottom right of the screen.

重新加载http://localhost:8000/polls/,你应该在屏幕的右下方看到载入的背景图片。

Warning
Of course the{% raw%} {% static %} {% endraw%} template tag is not available for use in static files like your stylesheet which aren’t generated by Django. You should always use relative paths to link your static files between each other, because then you can change STATIC_URL (used by the static template tag to generate its URLs) without having to modify a bunch of paths in your static files as well.

当然,{% static %}模板标签不能用在静态文件(比如样式表)中,因为他们不是由Django生成的。你应该永远使用相对路径来相互链接静态文件,因为这样你可以改变STATIC_URLstatic 模板标签用它来生成URLs)而不用同时修改一大堆静态文件的路径。

These are the basics. For more details on settings and other bits included with the framework see the static files howto and the staticfiles reference. Deploying static files discusses how to use static files on a real server.

这些就是静态文件的基础知识。关于静态文件设置的更多细节和框架中包含的其它部分,参见the static files howto and the staticfiles reference. Deploying static files 讨论如何在真实的服务器上使用静态文件。

When you’re comfortable with the static files, read part 7 of this tutorial to learn how to customize Django’s automatically-generated admin site.

当你熟悉了静态文件,请看 [教程7](https://www.gitbook.com/book/run-noob/django-chinese-docs-1-10/edit#/edit/master/First steps/Writing your first Django app part 7.md?_k=zk0rz8) 学习如何定制Django自动生成的admin站点。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容