django admin应用开发(1) 之 引言

第一节 引言

1.1 准备知识

Python&Django配置

Django官方主页:www.djangoproject.com

开发IDE,个人倾向于PyCharm,在这里可以快速的学习到一些Django使用技巧。

1.2 什么是应用(APP)

要想回答这个问题,首先要了解Django项目的结构。Django项目采用“项目(project)-应用(app)-模块(module)”的两级结构,其中app是一个实现某种功能的web程序,有自己的数据模型、视图、模板等。也就是说一个project可以包含多个app,一个app也可以在多个项目中使用。如果觉得自己写的app有一定的通用性,也可以将其发布。常见的Django第三方app有很多,可以从官方wiki(链接)上找。

1.3 admin应用

admin应用指的是django自带的admin应用,它的路径是django.contrib.admin

利用它可以自动生成后台管理界面,这也是Django强大的地方之一(个人觉得还有一点是部署方便,建立一个demo网站只需要python + django + runserver + sqlite3基本上可以搞定所有东西,不必想php一样装服务器什么)。在《The Django Book》对admin的描述是这样的:

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.

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.

(译文):

对于某一类网站,管理界面是基础设施中非常重要的一部分。 这是以网页和有限的可信任管理者为基础的界面,它可以让你添加,编辑和删除网站内容。 一些常见的例子:你可以用这个界面发布博客,后台的网站管理者用它来润色读者提交的内容,你的客户用你给他们建立的界面工具更新新闻并发布在网站上,这些都是使用管理界面的例子。

但是管理界面有一问题:创建它太繁琐。当你开发对公众的功能时,网页开发是有趣的,但是创建管理界面通常是千篇一律的。 你必须认证用户,显示并管理表格,验证输入的有效性诸如此类。 这很繁琐而且是重复劳动。

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


 

 

转载于:https://my.oschina.net/kinegratii/blog/217633

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值