已經(jīng)同步到gitbook,想閱讀的請轉(zhuǎn)到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,我們已經(jīng)創(chuàng)建了一個已被測試過的web投票應(yīng)用,并且我們也加了樣式和圖片
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”.
除了由服務(wù)器生成的HTML文件外,網(wǎng)頁應(yīng)用一般需要提供其它必要的文件 —— 比如圖片、JavaScript和CSS樣式表 —— 來為用戶呈現(xiàn)出一個完整的頁面。 在Django中,我們將這些文件稱為“靜態(tài)文件”。
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服務(wù)器可以訪問到的地方就可以了。 然而,在大一點的項目中 —— 尤其是那些由多個應(yīng)用組成的項目 —— 處理每個應(yīng)用提供的多個靜態(tài)文件集合開始變得很難。
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要解決的:它收集每個應(yīng)用(和任何你指定的地方)的靜態(tài)文件到一個單獨的位置,這個位置在線上可以很容易維護(hù)。
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中創(chuàng)建一個static目錄。Django將在那里查找靜態(tài)文件,這與Django在polls/templates/中尋找對應(yīng)的模板文件的方式是一樣的
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設(shè)置包含一個查找器,它們知道如何從各種源找到靜態(tài)文件。 其中默認(rèn)的一個是AppDirectoriesFinder,它在每個INSTALLED_APPS下查找“static”子目錄,就像剛剛在polls
中創(chuàng)建的一樣。管理站點也為它的靜態(tài)文件使用相同的目錄結(jié)構(gòu)。
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
在你剛剛創(chuàng)建的static目錄中,創(chuàng)建另外一個目錄polls并在它下面創(chuàng)建一個文件style.css。換句話講,你的樣式表應(yīng)該位于polls/static/polls/style.css。根據(jù)AppDirectoriesFinder 靜態(tài)文件查找器的原理,你可以通過polls/style.css在Django中訪問這個靜態(tài)文件,與你如何訪問模板的路徑類似。
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.
與模板類似,我們可以將靜態(tài)文件直接放在polls/static(而不是創(chuàng)建另外一個polls 子目錄),但實際上這是一個壞主意。Django將使用它所找到的第一個符合要求的靜態(tài)文件的文件名,如果在你的不同應(yīng)用中存在兩個同名的靜態(tài)文件,Django將無法區(qū)分它們。我們需要告訴Django該使用其中的哪一個,最簡單的方法就是為它們添加命名空間。 也就是說,將這些靜態(tài)文件放進(jìn)以它們所在的應(yīng)用的名字命名的另外一個目錄下。
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 %} 標(biāo)簽生成靜態(tài)文件的絕對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.
這就是你在開發(fā)過程中所需要對靜態(tài)文件做的所有處理。 重新加載http://localhost:8000/polls/,你應(yīng)該會看到Question的超鏈接變成了綠色(Django的風(fēng)格!),這意味著你的樣式表被成功導(dǎo)入。
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.
下一步,我們將創(chuàng)建一個子目錄來存放圖片。 在polls/static/polls/目錄中創(chuàng)建一個 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/,你應(yīng)該在屏幕的右下方看到載入的背景圖片。
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.
當(dāng)然,{% static %}模板標(biāo)簽不能用在靜態(tài)文件(比如樣式表)中,因為他們不是由Django生成的。你應(yīng)該永遠(yuǎn)使用相對路徑來相互鏈接靜態(tài)文件,因為這樣你可以改變STATIC_URL(static 模板標(biāo)簽用它來生成URLs)而不用同時修改一大堆靜態(tài)文件的路徑。
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.
這些就是靜態(tài)文件的基礎(chǔ)知識。關(guān)于靜態(tài)文件設(shè)置的更多細(xì)節(jié)和框架中包含的其它部分,參見the static files howto and the staticfiles reference. Deploying static files 討論如何在真實的服務(wù)器上使用靜態(tài)文件。
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.
當(dāng)你熟悉了靜態(tài)文件,請看 [教程7](https://www.gitbook.com/book/run-noob/django-chinese-docs-1-10/edit#/edit/master/First steps/Writing your ?rst Django app part 7.md?_k=zk0rz8) 學(xué)習(xí)如何定制Django自動生成的admin站點。