The Django Book

Chapter 1: Introduction to Django

绗竴绔 Django浠嬬垂

This book is about Django, a Web development framework that saves you time and makes Web development a joy. Using Django, you can build and maintain high-quality Web applications with minimal fuss.

鏈功鎵璁茬殑鏄疍jango锛氫竴娆捐兘澶熻妭绾︿綘鐨勬椂闂村苟涓旇浣犵殑寮鍙戜箰瓒fí鐢熺殑web寮鍙戞鏋躲備娇鐢―jango锛岃姳鏋佸皯鏃堕棿鍗冲彲鏋勫缓鍜岀淮鎶よ川閲忎笂涔樼殑Web搴旂敤銆

At its best, Web development is an exciting, creative act; at its worst, it can be a repetitive, frustrating nuisance. Django lets you focus on the fun stuff the crux of your Web application while easing the pain of the repetitive bits. In doing so, it provides high-level abstractions of common Web development patterns, shortcuts for frequent programming tasks, and clear conventions for how to solve problems. At the same time, Django tries to stay out of your way, letting you work outside the scope of the framework as needed.

浠庡ソ鐨勬柟闈㈡潵鐪嬶紝Web 寮鍙戞縺鍔ㄤ汉蹇冧笖瀵屼簬鍒涢犳э紱浠庡彟涓闈㈡潵鐪嬶紝瀹冨嵈鏄唤绻佺悙鑰屼护浜虹敓鍘岀殑宸ヤ綔銆傞氳繃鍑忓皯閲嶅鐨勪唬鐮侊紝Django 浣夸綘鑳藉涓撴敞浜 web 搴旂敤涓婃湁 瓒g殑鍏抽敭鎬х殑涓滆タ銆備负浜嗚揪鍒拌繖涓洰鏍囷紝Django 鎻愪緵浜嗛氱敤Web寮鍙戞ā寮忕殑楂樺害鎶借薄锛屾彁渚涗簡棰戠箒杩涜鐨勭紪绋嬩綔涓氱殑蹇熻В鍐虫柟娉曪紝浠ュ強涓衡滃浣曡В鍐抽棶棰樷濇彁渚涗簡娓呮櫚鏄庝簡鐨勭害瀹氥傚悓鏃讹紝Django 灏濊瘯鐣欎笅涓浜涙柟娉曪紝鏉ヨ浣犳牴鎹渶瑕佸湪framework涔嬪鏉ュ紑鍙戙

The goal of this book is to make you a Django expert. The focus is twofold. First, we explain, in depth, what Django does and how to build Web applications with it. Second, we discuss higher-level concepts where appropriate, answering the question How can I apply these tools effectively in my own projects? By reading this book, youll learn the skills needed to develop powerful Web sites quickly, with code that is clean and easy to maintain.

鏈功鐨勭洰鐨勬槸灏嗕綘鍩瑰吇鎴怐jango涓撳銆備富瑕佷晶閲嶄簬涓ゆ柟闈細绗竴锛屾垜浠繁搴﹁В閲 Django 鍒板簳鍋氫簡鍝簺宸ヤ綔浠ュ強濡備綍鐢ㄥス鏋勫缓Web搴旂敤锛涚浜岋紝鎴戜滑灏嗕細鍦ㄩ傚綋鐨勫湴鏂硅璁烘洿楂樼骇鐨勬蹇碉紝骞惰В閲婂浣曞湪鑷繁鐨勯」鐩腑楂樻晥鐨勪娇鐢ㄨ繖浜涘伐鍏枫傞氳繃闃呰姝や功锛屼綘灏嗗浼氬揩閫熷紑鍙戝姛鑳藉己澶х綉绔欑殑鎶宸э紝骞朵笖浣犵殑浠g爜灏嗕細鍗佸垎 娓呮櫚锛屾槗浜庣淮鎶ゃ

In this chapter, we provide a high-level overview of Django.

鍦ㄨ繖涓绔犱腑锛屾垜浠皢涓瑙 Django 鐨勫叏璨屻

What Is a Web Framework?

浠涔堟槸Web妗嗘灦?

Django is a prominent member of a new generation of Web frameworks . So what exactly does that term mean?

Django 鏄 Web妗嗘灦 涓潪甯稿嚭鑹茬殑鏂颁竴浠f垚鍛樸傞偅涔 Web 妗嗘灦杩欎釜鏈鐨勭‘鍒囧惈涔夊埌搴曟槸浠涔堝憿锛

To answer that question, lets consider the design of a Web application written using the Common Gateway Interface (CGI) standard, a popular way to write Web applications circa 1998. In those days, when you wrote a CGI application, you did everything yourself the equivalent of baking a cake from scratch. For example, heres a simple CGI script, written in Python, that displays the ten most recently published books from a database:

瑕佸洖绛旇繖涓棶棰橈紝璁╂垜浠潵鐪嬬湅閫氳繃缂栧啓鏍囧噯鐨凜GI绋嬪簭鏉ュ紑鍙慦eb搴旂敤锛岃繖鍦ㄥぇ绾1998骞寸殑鏃跺欓潪甯告祦琛屻傜紪鍐機GI Web搴旂敤鏃讹紝浣犻渶瑕佽嚜宸卞鐞嗘墍鏈夌殑鎿嶄綔锛屽氨鍍忎綘鎯崇儰闈㈠寘锛屼絾鏄兘闇瑕佽嚜宸辩敓鐏竴鏍枫備笅闈㈡槸瀹炰緥锛屼竴涓畝鍗曠殑CGI鑴氭湰锛岀敤Python鍐欑殑锛岃鍙栨暟鎹簱骞舵樉绀烘渶鏂板彂甯冪殑鍗佹湰涔︼細

#!/usr/bin/python

import MySQLdb

print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"

connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')
cursor = connection.cursor()
cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")
for row in cursor.fetchall():
    print "<li>%s</li>" % row[0]

print "</ul>"
print "</body></html>"

connection.close()

This code is straightforward. First, it prints a Content-Type line, followed by a blank line, as required by CGI. It prints some introductory HTML, connects to a database and executes a query that retrieves the latest ten books. Looping over those books, it generates an HTML unordered list. Finally, it prints the closing HTML and closes the database connection.

浠g爜鍗佸垎绠鍗曘傞鍏堬紝鏍规嵁CGI鐨勮姹傝緭鍑轰竴琛孋ontent-Type锛屾帴涓嬫潵鏄竴涓┖琛屻傚啀鎺ヤ笅 鏉ユ槸涓浜汬TML鐨勮捣濮嬫爣绛撅紝鐒跺悗杩炴帴鏁版嵁搴撳苟鎵ц涓浜涙煡璇㈡搷浣滐紝鑾峰彇鏈鏂扮殑鍗佹湰涔︺傞亶鍘嗚繖浜涗功锛屽悓鏃剁敓鎴愪竴涓 HTML鐨勬棤搴忓簭鍒椼傛渶鍚庯紝杈撳嚭HTML鐨勭粨鏉熸爣绛惧苟涓斿叧闂暟鎹簱杩炴帴銆

With a one-off dynamic page such as this one, the write-it-from-scratch approach isnt necessarily bad. For one thing, this code is simple to comprehend even a novice developer can read these 16 lines of Python and understand all it does, from start to finish. Theres nothing else to learn; no other code to read. Its also simple to deploy: just save this code in a file called latestbooks.cgi , upload that file to a Web server, and visit that page with a browser.

绫讳技杩欐牱鐨勪竴娆℃у姩鎬侀〉闈紝浠庡ご鍐欒捣骞朵笉鏄渶濂界殑鍔炴硶銆傚叾涓竴鐐癸紝杩欎簺浠g爜绠鍗曟槗鎳傦紝灏辩畻鏄竴涓紑鍙戝垵瀛﹁呴兘鑳界悊瑙h繖16琛屼唬鐮佷粠寮濮嬪埌缁撴潫鍋氫簡浜涗粈涔堬紝涓嶉渶瑕侀槄璇婚澶栫殑浠g爜銆傚悓鏍疯繖16琛屼唬鐮佷篃寰堝鏄撻儴缃诧細鍙渶瑕佸皢瀹冧繚瀛樺湪鍚嶄负鈥渓atestbooks.cgi鈥濈殑鏂囦欢閲岋紝涓婁紶鑷崇綉缁滄湇鍔″櫒锛岄氳繃娴忚鍣ㄨ闂嵆鍙

But as a Web application grows beyond the trivial, this approach breaks down, and you face a number of problems:

浣嗘槸Web搴旂敤杩滆繙瑕佸鏉傚緢澶氾紝杩欑鏂规硶灏变笉鍐嶉傜敤锛岃屼笖浣犲皢浼氳闈㈠寰堝闂锛

  • What happens when multiple pages need to connect to the database? Surely that database-connecting code shouldnt be duplicated in each individual CGI script, so the pragmatic thing to do would be to refactor it into a shared function.

  • 褰撳涓姩鎬侀〉闈㈤渶瑕佸悓鏃惰繛鎺ユ暟鎹簱鏃讹紝灏嗕細鍙戠敓浠涔堬紵褰撶劧锛岃繛鎺ユ暟鎹簱鐨勪唬鐮佷笉 搴旇鍚屾椂瀛樺湪浜庡悇涓嫭绔嬬殑CGI鑴氭湰涓紝鎵浠ユ渶韪忓疄鐨勫仛娉曟槸鎶婅繖浜涗唬鐮侀噸鏂扮粍缁囧埌涓涓叕鍏卞嚱鏁伴噷闈€

  • Should a developer really have to worry about printing the Content-Type line and remembering to close the database connection? This sort of boilerplate reduces programmer productivity and introduces opportunities for mistakes. These setup- and teardown-related tasks would best be handled by some common infrastructure.

  • 涓涓紑鍙戜汉鍛樼湡鐨勯渶瑕佸幓鍏虫敞濡備綍杈撳嚭Content-Type浠ュ強瀹屾垚鎵鏈夋搷浣滃悗鍘诲叧闂暟鎹簱涔堬紵姝ょ被闂鍙細闄嶄綆寮鍙戜汉鍛樼殑宸ヤ綔鏁堢巼锛屽鍔犵姱閿欒鐨勫嚑鐜囥傞偅浜涘垵濮嬪寲鍜岄噴鏀剧浉鍏崇殑宸ヤ綔搴旇浜ょ粰涓浜涢氱敤鐨勬鏋舵潵瀹屾垚銆

  • What happens when this code is reused in multiple environments, each with a separate database and password? At this point, some environment-specific configuration becomes essential.

  • 濡傛灉杩欐牱鐨勪唬鐮佽閲嶇敤鍒颁竴涓鍚堢殑鐜涓細鍙戠敓浠涔堬紵姣忎釜椤甸潰閮藉垎鍒搴旂嫭绔嬬殑鏁版嵁搴撳拰瀵嗙爜鍚楋紵浠庤繖鐐圭湅鏉ワ紝灏遍渶瑕佷竴浜涚幆澧冪浉鍏崇殑閰嶇疆鏂囦欢銆

  • What happens when a Web designer who has no experience coding Python wishes to redesign the page? Ideally, the logic of the page the retrieval of books from the database would be separate from the HTML display of the page, so that a designer could edit the latter without affecting the former.

  • 濡傛灉涓涓猈eb璁捐甯堬紝瀹屽叏娌℃湁Python寮鍙戠粡楠岋紝浣嗘槸鍙堥渶瑕侀噸鏂拌璁¢〉闈㈢殑璇濓紝鍙堝皢 鍙戠敓浠涔堝憿锛熺悊鎯崇殑鎯呭喌鏄紝椤甸潰鏄剧ず鐨勯昏緫涓庝粠鏁版嵁搴撲腑璇诲彇涔︽湰璁板綍鍒嗛殧寮锛岃繖鏍 Web璁捐甯堢殑閲嶆柊璁捐涓嶄細褰卞搷鍒颁箣鍓嶇殑涓氬姟閫昏緫銆

These problems are precisely what a Web framework intends to solve. A Web framework provides a programming infrastructure for your applications, so that you can focus on writing clean, maintainable code without having to reinvent the wheel. In a nutshell, thats what Django does.

浠ヤ笂姝f槸Web妗嗘灦鑷村姏浜庤В鍐崇殑闂銆俉eb妗嗘灦涓哄簲鐢ㄧ▼搴忔彁渚涗簡涓濂楃▼搴忔鏋讹紝 杩欐牱浣犲彲浠ヤ笓娉ㄤ簬缂栧啓娓呮櫚銆佹槗缁存姢鐨勪唬鐮侊紝鑰屾棤闇浠庡ご鍋氳捣銆傜畝鍗曟潵璇达紝杩欏氨鏄疍jango鎵鑳藉仛鐨勩

The MVC Design Pattern

MVC 璁捐妯″紡

Lets dive in with a quick example that demonstrates the difference between the previous approach and that undertaken using a Web framework. Heres how you might write the previous CGI code using Django:

璁╂垜浠潵鐮旂┒涓涓畝鍗曠殑渚嬪瓙锛岄氳繃璇ュ疄渚嬶紝浣犲彲浠ュ垎杈ㄥ嚭锛岄氳繃Web妗嗘灦鏉ュ疄鐜扮殑鍔熻兘涓庝箣鍓嶇殑鏂瑰紡鏈変綍涓嶅悓銆備笅闈㈠氨鏄氳繃浣跨敤Django鏉ュ畬鎴愪互涓婂姛鑳界殑渚嬪瓙锛

# models.py (the database tables)

from django.db import models

class Book(models.Model):
    name = models.CharField(maxlength=50)
    pub_date = models.DateField()


# views.py (the business logic)

from django.shortcuts import render_to_response
from models import Book

def latest_books(request):
    book_list = Book.objects.order_by('-pub_date')[:10]
    return render_to_response('latest_books.html', {'book_list': book_list})


# urls.py (the URL configuration)

from django.conf.urls.defaults import *
import views

urlpatterns = patterns('',
    (r'latest/$', views.latest_books),
)


# latest_books.html (the template)

<html><head><title>Books</title></head>
<body>
<h1>Books</h1>
<ul>
{% for book in book_list %}
<li>{{ book.name }}</li>
{% endfor %}
</ul>
</body></html>

Dont worry about the particulars of how this works just yet we just want you to get a feel for the overall design. The main thing to note here is the separation of concerns :

鍏堜笉瑕佹媴蹇冭繖涓笢瑗挎槸 濡備綍 宸ヤ綔鐨勶紝鎴戜滑涓昏鏄厛鎯宠浣犵煡閬撴讳綋鐨勮璁★紝杩欓噷鍏抽敭瑕佹敞鎰忕殑鏄 鍒嗙闂

  • The models.py file contains a description of the database table, as a Python class. This is called a model . Using this class, you can create, retrieve, update, and delete records in your database using simple Python code rather than writing repetitive SQL statements.

  • models.py 鏂囦欢涓昏鐢ㄤ竴涓 Python 绫绘潵鎻忚堪鏁版嵁琛ㄣ傜О涓 妯″瀷(model) 銆 杩愮敤杩欎釜绫伙紝浣犲彲浠ラ氳繃绠鍗曠殑 Python 鐨勪唬鐮佹潵鍒涘缓銆佹绱€佹洿鏂般佸垹闄 鏁版嵁搴撲腑鐨勮褰曡屾棤闇鍐欎竴鏉″張涓鏉$殑SQL璇彞銆

  • The views.py file contains the business logic for the page, in the latest_books() function. This function is called a view .

  • view.py 鏂囦欢鐨 latest_books() 鍑芥暟涓寘鍚簡璇ラ〉鐨勪笟鍔″眰閫昏緫銆傝繖涓嚱鏁板彨鍋 瑙嗗浘(view)

  • The urls.py file specifies which view is called for a given URL pattern. In this case, the URL /latest/ will be handled by the latest_books() function.

  • urls.py 鎸囧嚭浜嗕粈涔堟牱鐨 URL 璋冪敤浠涔堢殑瑙嗗浘锛屽湪杩欎釜渚嬪瓙涓 /latest/ URL 灏嗕細璋冪敤 latest_books() 杩欎釜鍑芥暟

  • The latest_books.html is an HTML template that describes the design of the page.

  • latest_books.html 鏄 html 妯℃澘锛屽畠鎻忚堪浜嗚繖涓〉闈㈢殑璁捐鏄浣曠殑銆

Taken together, these pieces loosely follow the Model-View-Controller (MVC) design pattern. Simply put, MVC defines a way of developing software so that the code for defining and accessing data (the model) is separate from request routing logic (the controller), which in turn is separate from the user interface (the view).

杩欎簺閮ㄥ垎鏉炬暎鐨勭粍鍚堝湪涓璧峰氨鏄ā鍨嬶紞瑙嗗浘锛嶆帶鍒跺櫒锛圡VC锛夌殑璁捐妯″紡銆傜畝鍗曠殑璇达紝 MVC 鏄竴绉嶈蒋浠跺紑鍙戠殑鏂规硶锛屽畠鎶婁唬鐮佺殑瀹氫箟鍜屾暟鎹闂殑鏂规硶锛堟ā鍨嬶級涓庤姹傞昏緫 锛堟帶鍒跺櫒锛夎繕鏈夌敤鎴锋帴鍙o紙瑙嗗浘锛夊垎寮鏉ャ

A key advantage of such an approach is that components are loosely coupled . That is, each distinct piece of a Django-powered Web application has a single key purpose and can be changed independently without affecting the other pieces. For example, a developer can change the URL for a given part of the application without affecting the underlying implementation. A designer can change a pages HTML without having to touch the Python code that renders it. A database administrator can rename a database table and specify the change in a single place, rather than having to search and replace through a dozen files.

杩欑璁捐妯″紡鍏抽敭鐨勪紭鍔垮湪浜庡悇绉嶇粍浠堕兘鏄 鏉炬暎缁撳悎 鐨勩傝繖鏍凤紝姣忎釜鐢 Django椹卞姩 鐨刉eb搴旂敤閮芥湁鐫鏄庣‘鐨勭洰鐨勶紝骞朵笖鍙嫭绔嬫洿鏀硅屼笉褰卞搷鍒板叾瀹冪殑閮ㄥ垎銆傛瘮濡傦紝寮鍙戣 鏇存敼涓涓簲鐢ㄧ▼搴忎腑鐨 URL 鑰屼笉鐢ㄥ奖鍝嶅埌杩欎釜绋嬪簭搴曞眰鐨勫疄鐜般傝璁″笀鍙互鏀瑰彉 HTML 椤甸潰 鐨勬牱寮忚屼笉鐢ㄦ帴瑙 Python 浠g爜銆傛暟鎹簱绠$悊鍛樺彲浠ラ噸鏂板懡鍚嶆暟鎹〃骞朵笖鍙渶鏇存敼涓涓湴鏂癸紝鏃犻渶浠庝竴澶у爢鏂囦欢涓繘琛屾煡鎵惧拰鏇挎崲銆

In this book, each component of this stack gets its own chapter. For example, Chapter 3 covers views, Chapter 4 covers templates, and Chapter 5 covers models. Chapter 5 also discusses Djangos MVC philosophies in depth.

鏈功涓紝姣忎釜缁勪欢閮芥湁瀹冭嚜宸辩殑涓涓珷鑺傘傛瘮濡傦紝绗笁绔犳兜鐩栦簡瑙嗗浘锛岀鍥涚珷鏄ā鏉匡紝 鑰岀浜旂珷鏄ā鍨嬨傚悓鏃剁浜旂珷涔熸繁鍏ヨ璁轰簡 Django 鐨 MVC 鎬濇兂銆

Djangos History

django 鍘嗗彶

Before we dive into more code, we should take a moment to explain Djangos history. Its helpful to understand why the framework was created, because a knowledge of the history will put into context why Django works the way it does.

鍦ㄦ垜浠璁轰唬鐮佷箣鍓嶆垜浠渶瑕佸厛浜嗚В涓涓 Django 鐨勫巻鍙层傜煡閬撲簡涓浜涘巻鍙茬煡璇嗘湁鍔╀簬鐞嗚В涓轰粈涔 Django 瑕佸缓绔嬭繖涓鏋讹紝鍥犱负杩欎簺鍘嗗彶鏈夊姪浜庣悊瑙jango涓轰綍浼氳繖鏍疯繍浣溿

If youve been building Web applications for a while, youre probably familiar with the problems in the CGI example we presented earlier. The classic Web developers path goes something like this:

濡傛灉浣犳浘缂栧啓杩囩綉缁滃簲鐢ㄧ▼搴忋傞偅涔堜綘寰堟湁鍙兘鐔熸倝涔嬪墠鎴戜滑鐨 CGI 渚嬪瓙銆備紶缁熺殑 缃戠粶寮鍙戜汉鍛樼殑寮鍙戞祦绋嬫槸杩欐牱鐨:

  1. Write a Web application from scratch.

  1. 浠庡ご寮濮嬬紪鍐欑綉缁滃簲鐢ㄧ▼搴忋

  1. Write another Web application from scratch.

  1. 浠庡ご缂栧啓鍙︿竴涓綉缁滃簲鐢ㄧ▼搴忋

  1. Realize the application from step 1 shares much in common with the application from step 2.

  1. 浠庣涓姝ヤ腑鎬荤粨锛堟壘鍑哄叾涓氱敤鐨勪唬鐮侊級锛屽苟杩愮敤鍦ㄧ浜屾涓

  1. Refactor the code so that application 1 shares code with application 2.

  1. 閲嶆瀯浠g爜浣垮緱鑳藉湪绗 2 涓▼搴忎腑浣跨敤绗 1 涓▼搴忎腑鐨勯氱敤浠g爜銆

  1. Repeat steps 2-4 several times.

  1. 閲嶅 2-4 姝ラ鑻ュ共娆°

  1. Realize youve invented a framework.

  1. 鎰忚瘑鍒颁綘鍙戞槑浜嗕竴涓鏋躲

This is precisely how Django itself was created!

杩欏氨鏄负浠涔 Django 鍒涘缓鐨勫師鍥狅紒

Django grew organically from real-world applications written by a Web development team in Lawrence, Kansas. It was born in the fall of 2003, when the Web programmers at the Lawrence Journal-World newspaper, Adrian Holovaty and Simon Willison, began using Python to build applications. The World Online team, responsible for the production and maintenance of several local news sites, thrived in a development environment dictated by journalism deadlines. For the sites including LJWorld.com, Lawrence.com, and KUsports.com journalists (and management) demanded that features be added and entire applications be built on an intensely fast schedule, often with only days or hours notice. Thus, Adrian and Simon developed a time-saving Web development framework out of necessity it was the only way they could build maintainable applications under the extreme deadlines.

Django 鏄粠鐪熷疄涓栫晫鐨勫簲鐢ㄤ腑鎴愰暱璧锋潵鐨勶紝瀹冩槸鐢 鍫惃鏂紙Kansas锛夊窞 Lawrence 鍩庝腑鐨勪竴涓 缃戠粶寮鍙戝皬缁勭紪鍐欑殑銆傚畠璇炵敓浜 2003 骞寸澶╋紝閭f椂 Lawrence Journal-World 鎶ョ焊鐨 绋嬪簭鍛 Adrian Holovaty 鍜 Simon Willison 寮濮嬬敤 Python 鏉ョ紪鍐欑▼搴忋 褰撴椂浠栦滑鐨 World Online 灏忕粍鍒朵綔骞剁淮鎶ゅ綋鍦扮殑鍑犱釜鏂伴椈绔欑偣, 骞跺湪浠ユ柊闂荤晫鐗规湁鐨勫揩鑺傚寮鍙戠幆澧冧腑閫愭笎鍙戝睍. 杩欎簺绔欑偣鍖呮嫭鏈 LJWorld.com銆丩awrence.com 鍜 KUsports.com锛 璁拌咃紙鎴栫鐞嗗眰锛 瑕佹眰澧炲姞鐨勭壒寰佹垨鏁翠釜绋嬪簭閮借兘鍦ㄨ鍒掓椂闂村唴蹇熺殑琚缓绔嬶紝杩欎簺鏃堕棿閫氬父鍙湁鍑犲ぉ 鎴栧嚑涓皬鏃躲傚洜姝や负浜嗛渶瑕侊紝Adrian 鍜 Simon 寮鍙戜簡涓绉嶈妭鐪佹椂闂寸殑缃戠粶绋嬪簭寮鍙戞鏋讹紝 杩欐槸鍦ㄦ埅姝㈡椂闂村墠鑳藉畬鎴愮▼搴忕殑鍞竴閫斿緞銆

In summer 2005, after having developed this framework to a point where it was efficiently powering most of World Onlines sites, the World Online team, which now included Jacob Kaplan-Moss, decided to release the framework as open source software. They released it in July 2005 and named it Django, after the jazz guitarist Django Reinhardt.

2005 骞寸殑澶忓ぉ锛屽綋杩欎釜妗嗘灦寮鍙戝畬鎴愭椂锛屽畠宸茬粡鐢ㄦ潵鍒朵綔浜嗗緢澶氫釜 World Online 鐨勭珯鐐广 褰撴椂 World Online 灏忕粍涓殑 Jacob Kaplan-Moss 鍐冲畾鎶婅繖涓鏋跺彂甯冧负涓涓紑婧愯蒋浠躲 浠栦滑鍦 2005 骞寸殑 7 鏈堝彂甯冨苟鍙栧悕涓 Django锛屾潵婧愪簬涓涓憲鍚嶇殑鐖靛+涔愬悏浠栨紨濂忓 Django Reinhardt銆

Although Django is now an open source project with contributors across the planet, the original World Online developers still provide central guidance for the frameworks growth, and World Online contributes other important aspects such as employee time, marketing materials, and hosting/bandwidth for the frameworks Web site (http://www.djangoproject.com/).

铏界劧鐜板湪 Django 鏄竴涓叏涓栫晫寮鍙戣呭弬涓庣殑寮婧愰」鐩紝浣嗗師濮嬬殑 World Online 寮鍙戣呬滑 浠嶇劧鎻愪緵涓昏鐨勬寚瀵兼潵淇冭繘杩欎釜妗嗘灦鐨勬垚闀裤 World Online 杩樻湁鍏跺畠鏂归潰鐨勯噸瑕佽础鐚紝姣斿闆囧憳鏃堕棿銆 甯傚満鏉愭枡浠ュ強妗嗘灦鐨 Web 缃戠珯鐨勪富鏈哄拰甯﹀(http://www.djangoproject.com/)銆

This history is relevant because it helps explain two key matters. The first is Djangos sweet spot. Because Django was born in a news environment, it offers several features (particularly its admin interface, covered in Chapter 6) that are particularly well suited for content sites sites like eBay, craigslist.org, and washingtonpost.com that offer dynamic, database-driven information. (Dont let that turn you off, though although Django is particularly good for developing those sorts of sites, that doesnt preclude it from being an effective tool for building any sort of dynamic Web site. Theres a difference between being particularly effective at something and being ineffective at other things.)

杩欎簺鍘嗗彶閮芥槸鐩稿叧鑱旂殑锛屽洜涓哄ス浠府鍔╄В閲婁簡寰堥噸瑕佺殑涓ょ偣銆傜涓锛孌jango鏈鍙埍鐨勫湴鏂癸紝 鍥犱负Django璇炵敓浜庝竴涓柊闂荤幆澧冿紝濂规彁渚涗簡寰堝鐨勫姛鑳斤紙鐗瑰埆鏄ス鐨勭鐞嗘帴鍙o紝瑙佺6绔狅級锛 鐗瑰埆閫傚悎鎻愪緵鍐呭鐨勭綉绔欙紝渚嬪eBay, craigslist.org鍜寃ashingtonpost.com锛屾彁渚涗竴绉 鍩轰簬鏁版嵁搴撶殑鍔ㄦ佺綉绔欍傦紙涓嶈鐪嬪埌杩欏氨鎰熷埌娌抚锛屽敖绠jango鎿呴暱浜庡姩鎬佸唴瀹圭鐞嗙郴缁燂紝 浣嗗苟涓嶈〃绀篋jango涓昏鐨勭洰鐨勫氨鏄敤鏉ュ垱寤哄姩鎬佸唴瀹圭殑缃戠珯銆 鏌愪簺鏂归潰 鐗瑰埆楂樻晥 涓 鍏朵粬鏂归潰 涓嶉珮鏁 鏄湁鍖哄埆鐨勶級

The second matter to note is how Djangos origins have shaped the culture of its open source community. Because Django was extracted from real-world code, rather than being an academic exercise or commercial product, it is acutely focused on solving Web development problems that Djangos developers themselves have faced and continue to face. As a result, Django itself is actively improved on an almost daily basis. The frameworks developers have a keen interest in making sure Django saves developers time, produces applications that are easy to maintain, and performs well under load. If nothing else, the developers are motivated by their own selfish desires to save themselves time and enjoy their jobs. (To put it bluntly, they eat their own dog food.)

绗簩锛孌jango鐨勮捣婧愰犲氨濂圭殑寮婧愮ぞ鍖猴紝鍥犱负Django鏉ヨ嚜浜庣湡瀹炰笘鐣屼腑鐨勪唬鐮侊紝鑰屼笉鏄 鏉ヨ嚜浜庝竴涓鐮旈」鐩垨鑰呭晢涓氫骇鍝侊紝濂逛富瑕侀泦涓姏閲忔潵瑙e喅Web寮鍙戜腑閬囧埌鐨勯棶棰橈紝鍚屾牱 涔熸槸Django鐨勫紑鍙戣呯粡甯搁亣鍒扮殑闂銆傝繖鏍凤紝Django姣忓ぉ鍦ㄧ幇鏈夌殑鍩虹涓婅繘姝ャ傛鏋剁殑 寮鍙戣呭浜庝负寮鍙戜汉鍛樿妭鐪佸紑鍙戞椂闂村叿鏈夋瀬澶х殑鍏磋叮锛岀紪鍐欐洿鍔犲鏄撶淮鎶ょ殑绋嬪簭锛屽悓鏃 淇濊瘉绋嬪簭杩愯鐨勬晥鐜囥傚紑鍙戜汉鍛樿嚜鎴戞縺鍔憋紝灏介噺鐨勮妭鐪佹椂闂村拰浜彈浠栦滑鐨勫伐浣(To put it bluntly, they eat their own dog food.)

How to Read This Book

濡備綍闃呰鏈功

In writing this book, we tried to strike a balance between readability and reference, with a bias toward readability. Our goal with this book, as stated earlier, is to make you a Django expert, and we believe the best way to teach is through prose and plenty of examples, rather than a providing an exhaustive but bland catalog of Django features. (As someone once said, you cant expect to teach somebody how to speak merely by teaching them the alphabet.)

鍦ㄧ紪鍐欐湰涔︽椂锛屾垜浠姫鍔涘皾璇曞湪鍙鎬у拰鍙傝冩ч棿鍋氫竴涓钩琛★紝褰撶劧鏈功浼氬亸鍚戜簬鍙 璇绘с傛湰涔︾殑鐩爣锛屼箣鍓嶄篃鎻愯繃锛屾槸瑕佸皢浣犲煿鍏绘垚涓鍚岲jango涓撳锛屾垜浠浉淇★紝鏈濂 鐨勬柟寮忓氨鏄枃绔犲拰鍏呰冻鐨勫疄渚嬶紝鑰屼笉鏄彁渚涗竴鍫嗚灏藉嵈涔忓懗鐨勫叧浜嶥jango鐗硅壊鐨勬墜鍐屻 锛堟浘缁忔湁浜鸿杩囷紝濡傛灉浠呬粎鏁欏瓧姣嶈〃鏄棤娉曟暀浼氬埆浜鸿璇濈殑銆傦級

With that in mind, we recommend that you read Chapters 1 through 7 in order. They form the foundation of how to use Django; once youve read them, youll be able to build Django-powered Web sites. The remaining chapters, which focus on specific Django features, can be read in any order.

鎸夌収杩欑鎬濊矾锛屾垜浠帹鑽愭寜椤哄簭闃呰绗 1-7 绔犮傝繖浜涚珷鑺傛瀯鎴愪簡濡備綍浣跨敤 Django 鐨勫熀纭锛涜杩囦箣鍚庯紝浣犲氨鍙互鎼缓鐢 Django 鏀拺鐨勭綉绔欎簡銆傚墿涓嬬殑绔犺妭閲嶇偣璁茶堪 Django 鐨勫叾瀹冧竴浜涚壒鎬э紝鍙互鎸夌収浠讳綍椤哄簭闃呰銆

The appendixes are for reference. They, along with the free documentation at http://www.djangoproject.com/, are probably what youll flip back to occasionally to recall syntax or find quick synopses of what certain parts of Django do.

闄勫綍閮ㄥ垎鐢ㄤ綔鍙傝冭祫鏂欍傝鍥炲繂璇硶鎴栨煡闃 Django 鏌愰儴鍒嗙殑鍔熻兘姒傝鏃讹紝浣犲伓灏斿彲鑳戒細鍥炴潵缈荤炕杩欎簺璧勬枡浠ュ強 http://www.djangoproject.com/ 涓婄殑鍏嶈垂鏂囨。銆

Required Programming Knowledge

鎵闇缂栫▼鐭ヨ瘑

Readers of this book should understand the basics of procedural and object-oriented programming: control structures (if , while , and for ), data structures (lists, hashes/dictionaries), variables, classes, and objects.

鏈功璇昏呴渶瑕佺悊瑙e熀鏈殑闈㈠悜杩囩▼鍜岄潰鍚戝璞$紪绋嬶細娴佺▼鎺у埗锛 ifwhilefor 锛夛紝鏁版嵁缁撴瀯锛堝垪琛紝鍝堝笇琛/瀛楀吀锛夛紝鍙橀噺锛岀被鍜屽璞°

Experience in Web development is, as you may expect, very helpful, but its not required to read this book. Throughout the book, we try to promote best practices in Web development for readers who lack this type of experience.

Web寮鍙戠粡楠岋紝姝e浣犳墍鎯崇殑锛屼篃鏄潪甯告湁甯姪鐨勶紝浣嗘槸瀵逛簬闃呰鏈功锛屽苟涓嶆槸蹇呴』鐨勩 閫氳繃鏈功锛屾垜浠敖閲忕粰缂轰箯缁忛獙鐨勫紑鍙戜汉鍛樻彁渚涘湪Web寮鍙戜腑鏈濂界殑瀹炶返銆

Required Python Knowledge

鎵闇python鐭ヨ瘑

At its core, Django is simply a collection of libraries written in the Python programming language. To develop a site using Django, you write Python code that uses these libraries. Learning Django, then, is a matter of learning how to program in Python and understanding how the Django libraries work.

鏈川涓婃潵璇达紝 Django 鍙笉杩囨槸鐢 Python 缂栧啓鐨勪竴缁勭被搴撱傜敤 Django 寮鍙戠珯鐐瑰氨鏄娇鐢ㄨ繖浜涚被搴撶紪鍐 Python 浠g爜銆傚洜姝わ紝瀛︿範 Django 鐨勫叧閿氨鏄涔犲浣曡繘琛 Python 缂栫▼骞剁悊瑙 Django 绫诲簱鐨勮繍浣滄柟寮忋

If you have experience programming in Python, you should have no trouble diving in. By and large, the Django code doesnt perform black magic (i.e., programming trickery whose implementation is difficult to explain or understand). For you, learning Django will be a matter of learning Djangos conventions and APIs.

濡傛灉浣犳湁Python寮鍙戠粡楠岋紝鍦ㄥ涔犺繃绋嬩腑搴旇涓嶄細鏈変换浣曢棶棰橈紝鍩烘湰涓,Django鐨勪唬鐮佸苟 娌℃湁浣跨敤涓浜涢粦鑹查瓟娉曪紙渚嬪浠g爜涓殑娆洪獥琛屼负锛屾煇涓疄鐜拌В閲婃垨鑰呯悊瑙h捣鏉ュ崄鍒嗗洶闅撅級銆 瀵逛綘鏉ヨ锛屽涔燚jango灏辨槸瀛︿範濂圭殑鍛藉悕瑙勫垯鍜孉PI銆

If you dont have experience programming in Python, youre in for a treat. Its easy to learn and a joy to use! Although this book doesnt include a full Python tutorial, it highlights Python features and functionality where appropriate, particularly when code doesnt immediately make sense. Still, we recommend you read the official Python tutorial, available online at http://docs.python.org/tut/. We also recommend Mark Pilgrims free book Dive Into Python , available at http://www.diveintopython.org/ and published in print by Apress.

濡傛灉浣犳病鏈変娇鐢 Python 缂栫▼鐨勭粡楠岋紝浣犱竴瀹氫細瀛﹀埌寰堝涓滆タ銆傚畠鏄潪甯告槗瀛︽槗鐢ㄧ殑銆 铏界劧杩欐湰涔︽病鏈夊寘鎷竴涓畬鏁寸殑 Python 鏁欑▼锛 浣嗕篃绠楁槸涓涓伆褰撶殑浠嬬粛浜 Python鐗瑰緛鍜 鍔熻兘鐨勯泦閿︺傚綋鐒讹紝鎴戜滑鎺ㄨ崘浣犺涓涓嬪畼鏂圭殑 Python 鏁欑▼锛屽畠鍙 浠ヤ粠 http://docs.python.org/tut/ 鍦ㄧ嚎鑾峰緱銆傚彟澶栨垜浠篃鎺ㄨ崘 Mark Pilgrims鐨 涔 Dive Into Pythonhttp://www.diveintopython.org/

New Django Features

Django 涔嬫柊鐗规

As we noted earlier, Django is frequently improved, and it will likely have a number of useful even essential new features by the time this book is published. Thus, our goal as authors of this book is twofold:

姝e鎴戜滑涔嬪墠鎻愬埌鐨勶紝Django 鏀硅繘棰戠箒锛屽埌鏈功鍑虹増鏃讹紝鍙兘鏈変竴浜涚敋鑷虫槸 闈炲父鍩烘湰 鐨勬柊鍔熻兘灏嗚鎺ㄥ嚭銆傚洜姝わ紝浣滀负浣滆咃紝鎴戜滑瑕侀氳繃姝や功杈惧埌涓や釜鏂归潰鐨勭洰鏍囷細

  • Make sure this book is as future-proof as possible, so that whatever you read here will still be relevant in future Django versions

  • 淇濊瘉鏈功灏藉彲鑳界殑闈㈠悜鏈潵锛屽洜姝わ紝涓嶇浣犲湪鏈功涓鍒颁粈涔堝唴瀹癸紝鍦ㄦ湭鏉ユ柊 鐨凞jango鐗堟湰涓兘灏嗕細鍙敤鐨勩

  • Actively update this book on its Web site, http://www.djangobook.com/, so you can access the latest and greatest documentation as soon as we write it

  • 鍙婃椂鐨勬洿鏂版湰涔︾殑鍦ㄧ嚎鐗堬紝 http://www.djangobook.com/ 锛岃繖鏍峰湪鎴戜滑瀹屾垚 鏂扮殑绔犺妭鍚庯紝浣犲彲浠ヨ幏寰楁渶鏂板拰鏈濂界殑鐗堟湰銆

If you want to implement something with Django that isnt explained in this book, check the latest version of this book on the aforementioned Web site, and also check the official Django documentation.

濡傛灉浣犳兂鐢╠jango鏉ュ疄鐜版煇浜涗功涓病鏈夋彁鍒扮殑鍔熻兘锛岃鍒板墠闈㈡彁鍒扮殑缃戠珯涓婃鏌ヨ繖鏈功 鐨勫湴鏈鏂扮増鏈紝骞朵笖鍚屾牱璁板緱瑕佸幓妫鏌ュ畼鏂圭殑django鏂囨。銆

Getting Help

鑾峰彇甯姪

One of the greatest benefits of Django is its kind and helpful user community. For help with any aspect of Django from installation, to application design, to database design, to deployment feel free to ask questions online.

django鐨勬渶澶х殑鐩婂鏄,鏈変竴缇や箰浜庡姪浜虹殑浜哄湪django绀惧尯涓.浣犲彲浠ユ鏃犵害鏉熺殑鎻愬悇绉 闂鍦ㄤ笂闈㈠:浠巇jango鐨勫畨瑁,app 璁捐,db 璁捐,鍙戝竷.

  • The Django IRC channel is where Django users hang out to chat and help each other in real time. Join the fun by logging on to #django on the Freenode IRC network.

  • django irc channel濡傛灉django鐢ㄦ埛閬囧埌浠涔堟鎵嬬殑闂甯屾湜鐨勫強鏃跺湴鍥炲鏄彲浠ヤ娇鐢ㄥ畠銆 鍦╢reenode IRC network鍔犲叆#django

Whats Next

涓嬩竴绔

In the next chapter, well get started with Django, covering installation and initial setup.

涓嬩竴绔狅紝涓昏璁茶堪濡備綍寮濮嬪畨瑁呭拰鍒濆鍖杁jango銆

Copyright 2006 Adrian Holovaty and Jacob Kaplan-Moss.
This work is licensed under the GNU Free Document License.
Hosting graciously provided by media temple
Chinese translate hosting by py3k.cn.