django admin應用開發(1) 之 引言

第一節 引言

1.1 準備知識

Python&Django配置php

Django官方主頁:www.djangoproject.comhtml

開發IDE,我的傾向於PyCharm,在這裏能夠快速的學習到一些Django使用技巧。python

1.2 什麼是應用(APP)

要想回答這個問題,首先要了解Django項目的結構。Django項目採用「項目(project)-應用(app)-模塊(module)」的兩級結構,其中app是一個實現某種功能的web程序,有本身的數據模型、視圖、模板等。也就是說一個project能夠包含多個app,一個app也能夠在多個項目中使用。若是以爲本身寫的app有必定的通用性,也能夠將其發佈。常見的Django第三方app有不少,能夠從官方wiki(連接)上找。web

1.3 admin應用

admin應用指的是django自帶的admin應用,它的路徑是django.contrib.adminsql

利用它能夠自動生成後臺管理界面,這也是Django強大的地方之一(我的以爲還有一點是部署方便,創建一個demo網站只須要python + django + runserver + sqlite3基本上能夠搞定全部東西,沒必要想php同樣裝服務器什麼)。在《The Django Book》對admin的描述是這樣的:django

For a certain class of Web sites, an admin interface is an essential part of the infrastructure. This is a Web-based interface, limited to trusted site administrators, that enables the adding, editing and deletion of site content. Some common examples: the interface you use to post to your blog, the backend site managers use to moderate user-generated comments, the tool your clients use to update the press releases on the Web site you built for them.服務器

There’s a problem with admin interfaces, though: it’s boring to build them. Web development is fun when you’re developing public-facing functionality, but building admin interfaces is always the same. You have to authenticate users, display and handle forms, validate input, and so on. It’s boring, and it’s repetitive.併發

So what’s Django’s approach to these boring, repetitive tasks? It does it all for you – in just a couple of lines of code, no less. With Django, building an admin interface is a solved problem.app

This chapter is about Django’s automatic admin interface. The feature works by reading metadata in your model to provide a powerful and production-ready interface that site administrators can start using immediately. Here, we discuss how to activate, use, and customize this feature.less

(譯文):

對於某一類網站,管理界面是基礎設施中很是重要的一部分。 這是以網頁和有限的可信任管理者爲基礎的界面,它可讓你添加,編輯和刪除網站內容。 一些常見的例子:你能夠用這個界面發佈博客,後臺的網站管理者用它來潤色讀者提交的內容,你的客戶用你給他們創建的界面工具更新新聞併發布在網站上,這些都是使用管理界面的例子。

可是管理界面有一問題:建立它太繁瑣。當你開發對公衆的功能時,網頁開發是有趣的,可是建立管理界面一般是千篇一概的。 你必須認證用戶,顯示並管理表格,驗證輸入的有效性諸如此類。 這很繁瑣並且是重複勞動。

Django 在對這些繁瑣和重複的工做進行了哪些改進?它用不能再少的代碼爲你作了全部的一切。Django 中建立管理界面已經不是問題。

這一章是關於 Django 的自動管理界面。這個特性是這樣起做用的: 它讀取你模式中的元數據,而後提供給你一個強大並且可使用的界面,網站管理者能夠用它當即工做


使用admin應用只須要如下幾個步驟:

  1. [settings/urls]配置相關的urls.py和settings.py,這一步一般是註釋相關代碼

  2. [models]定義本身的model

  3. [admin]爲每個model寫一個ModelAdmin,並自定義本身的行爲、UI、邏輯。

  4. [admin]將ModelAdmin和model關聯,使用admin.site.register方法


1.4 The Django Book

《The Django Book》在線文檔

中文:http://djangobook.py3k.cn/2.0/chapter06/

英文:http://www.djangobook.com/en/2.0/chapter06.html

相關文章
相關標籤/搜索