没有合适的资源?快使用搜索试试~ 我知道了~
首页Django开发精要:URL配置与通用视图
"本资源是一份关于Django框架的学习资料,着重讲解了Django的开发技术,特别是URL配置和通用视图的核心概念。作者Marty Alchin在《Pro Django》一书中强调了URL设计在应用整体架构中的重要性,并提到了Django的可维护性和灵活性。"
在Django开发中,URL配置扮演着至关重要的角色。通过提供正则表达式作为URL组件,开发者可以以高度可读和易于维护的方式处理各种具体的URL模式。这种方式允许一个视图在多个不同的URL下被配置,可能在每个位置有不同的选项。这种分离的配置方式使得视图可以在不修改本身代码的情况下,根据需要在不同的URL路径下进行部署。
Django的通用视图(Generic Views)是其核心特性之一。这些视图是为常见的需求设计的,具备可配置的选项,可以在任何应用中复用,只需要简单的URL配置就可以启用。这大大简化了开发过程,降低了代码的复杂度,同时也提高了代码的重用性。
书中指出,将URL设计视为应用程序整体设计的一部分是极其重要的。由于URL会被用于书签、博客文章和市场营销活动中,它们有时比应用本身更显眼。用户在决定访问网站之前,首先看到的就是URL。在传统媒体如印刷广告中,URL的重要性更是不言而喻。
此外,书中的内容可能还涵盖了对版权、商标的法律声明,以及编辑团队和技术审查者的贡献,但这些并非直接的技术知识点,而是出版流程和版权方面的信息。
Django学习资料中涉及的关键知识点包括:
1. Django的URL配置机制:使用正则表达式处理多种URL模式,提高灵活性和可读性。
2. 通用视图(Generic Views):为常见需求设计的可配置视图,便于在不同URL下复用。
3. URL设计原则:URL应被视为应用设计的重要组成部分,考虑到其在各种场景下的可见性和重要性。
4. 可维护性:Django通过分离URL配置和视图,增强了代码的可维护性。
通过深入学习和理解这些知识点,开发者可以更好地利用Django框架构建高效、可扩展且用户体验良好的Web应用。
xix
Introduction
Pro Django represents two and a half years of accumulated knowledge in Python and Django,
designed to educate readers who are already familiar with both topics and would like to take
them further than they had previously done. You will learn a wide range of advanced tech-
niques available in both Python and Django, along with tips on how to use them to achieve
advanced functionality.
This book is designed to be both a narrative to be read from start to finish and a general
reference to be searched for specific information. Since you may not know what to look for or
where to find it yet, feel free to read through the book first, then keep it handy for refreshing
your memory as necessary.
What This Book Is Not
There are plenty of resources available for learning Python and Django, so this book does not
strive to teach the basics. For readers new to Python, I highly recommend Dive Into Python
by Mark Pilgrim (Apress, 2004). For learning Django, I’d recommend The Definitive Guide to
Django: Web Development Done Right by Adrian Holovaty and Jacob Kaplan-Moss (Apress,
2006). Additionally, Practical Django Projects by James Bennett (Apress, 2008) is an excellent
resource for general application development.
Who This Book Is For
Because Pro Django doesn’t dwell on introductory details, readers will be expected to have
experience with both Python and Django. If you’re new to either subject, please consider one
of the books mentioned in the previous section before trying to tackle this book.
Even if you’ve only experimented on your own without launching a full site yet, a basic
familiarity should be sufficient. You don’t need to be an expert to start reading Pro Django, but
you might be by the time you finish.
Interpreting Code Samples
Pro Django uses a simple format, interleaving explanations of Python’s and Django’s available
features with code that demonstrates their use in the real world. There are two types of code
samples used, which differ in how they should be executed.
Python’s interactive interpreter is a great way to test out small pieces of code and see
how it works in a variety of situations. Lines of code intended for use in that environment will
always be prefixed with three characters: three greater-than signs (:::) or three periods (***).
Lines with greater-than signs are the outermost block of code, while the period-prefixed lines
are indented at least one level. The three initial characters are also followed by a space. These
INTRODUCTION
xx
first four characters are not typed into the interactive interpreter directly; they simply mimic
what the interpreter itself looks like by reproducing its output.
A line started with three periods but containing no other text indicates that you should
simply press Enter on a blank line in the interpreter. This completes any open code blocks,
bringing you back to the ::: prompt. Any lines that don’t begin with either ::: or *** repre-
sent the output of the code or the result of the previous expression.
:::eilknp`f]jck
:::lnejp`f]jck*cap[ranoekj$%
q#-*,)bej]h#
The first line of an interactive example will always begin with :::; everything else is code
that should be written in a file and executed as part of a running Django application. The sur-
rounding text will indicate what file the code should be placed in and how it will execute.
Prerequisites
Pro Django is written for Django 1.0, which was released on September 3, 2008. That release or
a more recent checkout from the Django code repository is required for the code samples to
work properly. Since Django in turn relies on Python, these examples also assume a working
Python environment of version 2.3 or higher.
1
CHAPTER 1
Understanding Django
Code alone isn’t enough. Sure, it’s what the computer runs, but code has to come from
somewhere. A programmer has to sit down and decide what features to include, how they
should be implemented, what other software to utilize and how to provide hooks for future
enhancements to be added. It’s easy to skip straight to code, ignoring the cognitive process
that produces it, but great programmers always have reasons for the decisions they make.
With a framework, like Django, many such decisions have already been made, and the
tools provided are shaped by these decisions and by the programmers who made them. By
adopting these philosophies in your own code, not only will you be consistent with Django
and other applications, but you may even be amazed at what you’re able to accomplish.
Beneath even the most fundamental code is the thought process that went into its cre-
ation. Decisions were made about what it should do and how it should do it. This thought
process is a step often overlooked in books and manuals, leading to an army of technicians
slaving away, writing code that manages to accomplish the task at hand but without a vision
for its future.
While the rest of this book will explain in detail the many basic building blocks Django
provides for even the most complicated of projects, this chapter will focus on these even more
fundamental aspects of the framework. For those readers coming from other backgrounds, the
ideas presented in this chapter may seem considerably foreign, but that doesn’t make them
any less important. All programmers working with Python and Django would do well to have
a
solid understanding of the reasons Django works the way it does, and how those principles
can be applied to other projects.
You may want to read this chapter more than once, and perhaps refer to it often as you
work with Django. Many of the topics covered in this chapter are common knowledge in the
Django community, so reading this chapter carefully is essential if you plan to interact with
other programmers.
Philosophy
Django relies heavily on philosophy, both in how its code is written and how decisions are
made about what goes into the framework. This isn’t unique in programming, but it’s some-
thing newcomers often have trouble with. It is essential to maintain both consistency and
quality, and having a set of common philosophies to refer to when making decisions helps
maintain both. Since these concepts are also important to individual applications, and even
collections of applications, a firm grasp on these philosophies will yield similar benefits.
CHAPTER 1 UNDERSTANDING DJANGO
2
Perhaps the best- known and most- quoted passage of Python philosophy comes from Tim
Peters, a longtime Python guru who wrote down many of the principles that guide Python’s own
development process. The 19 lines he came up with have been so influential to Python program-
mers over time that they are immortalized as Python Enhancement Proposal (PEP) 20
1
and in
the Python distribution itself, as an “easter egg” module called pdeo.
:::eilknppdeo
>a]qpebqheo^appanpd]jqchu*
Atlhe_epeo^appanpd]jeilhe_ep*
Oeilhaeo^appanpd]j_kilhat*
?kilhateo^appanpd]j_kilhe_]pa`*
Bh]peo^appanpd]jjaopa`*
Ol]noaeo^appanpd]j`ajoa*
Na]`]^ehepu_kqjpo*
Ola_e]h_]oao]naj#pola_e]hajkqcdpk^na]gpdanqhao*
=hpdkqcdln]_pe_]hepu^a]polqnepu*
Annknoodkqh`jaranl]oooehajphu*
Qjhaooatlhe_ephuoehaj_a`*
Ejpdab]_akb]i^ecqepu(nabqoapdapailp]pekjpkcqaoo*
Pdanaodkqh`^akja))]j`lnaban]^hukjhukja))k^rekqos]upk`kep*
=hpdkqcdpd]ps]ui]ujkp^ak^rekqo]pbenopqjhaooukq#na@qp_d*
Jkseo^appanpd]jjaran*
=hpdkqcdjaraneokbpaj^appanpd]j&necdp&jks*
Ebpdaeilhaiajp]pekjeod]n`pkatlh]ej(ep#o]^]`e`a]*
Ebpdaeilhaiajp]pekjeoa]oupkatlh]ej(epi]u^a]ckk`e`a]*
J]iaol]_ao]nakjadkjgejccna]pe`a]))hap#o`kiknakbpdkoa
While some of this is clearly intended for humor, the majority is a good summation of
many Python philosophies. The remainder of this chapter highlights some specific principles
that are often cited within the Django community, but all professional Python programmers
should keep this text in mind and reference it often.
One important thing to keep in mind is that many of the lines in the Zen of Python are
subjective. For example, while “beautiful” may be better than “ugly,” definitions of “beauti-
ful” are plentiful and can vary as much as the people who provide them. Similarly, consider
notions of simplicity and complexity, practicality and purity; each developer will have a differ-
ent opinion on which side of the line a particular piece of code should be placed.
Django’s Interpretation of the MVC Pattern
One of the most common application architectures—as adopted by hobbyists and corporations
alike—is the Model-View- Controller (MVC) pattern, as it provides clean separation of tasks and
responsibilities among the prominent aspects of an application. Django only loosely follows
this model. A proper discussion should kick off with a quick overview of its components.
1 dppl6++lnk`f]jck*_ki+lal).,+
CHAPTER 1 UNDERSTANDING DJANGO
3
While this pattern has proven very effective in many domains, Django’s authors weren’t
looking to conform to any form of pattern at the outset. They were simply interested in finding
the most effective way to develop software for the Web. After all, Django was built for the daily
needs of a working newspaper, where things have to happen very quickly if they’re to happen
at all. Ultimately, the separation of tasks into discrete groups serves a few different purposes.
doesn’t need to make assumptions about completely unrelated parts of the application.
-
ferent view and controller layers may connect to a single model layer. This enables
a
variety of applications to share the same business logic and data, presenting it and
interacting with it in different ways, for different audiences.
work being performed. This specialization helps to curb frustration and fatigue, while
fostering creativity and excellence within each developer’s domain of specialty.
There are certainly other smaller benefits, but these are generally the main goals achieved
with the use of MVC. It’s interesting to note, however, that the only part of those benefits that
applies to any specific division in the MVC pattern is the ability to plug multiple applications
into a single model layer. The rest is just an arbitrary division based on common development
plans.
Django’s developers sought these same benefits, but with an emphasis on rapid development,
without worrying about creating a development pattern. After getting a set of tools that made sense
for their workflow, they ended up with what some have called a Model-Template- View (MTV)
pattern. However, there are really four primary code divisions in a Django application, which are
outlined next.
Model
Given the benefit of keeping models apart from the rest of the application, Django follows that
part of MVC to the letter. Django models provide easy access to an underlying data storage
mechanism, and can also encapsulate any core business logic, which must always remain in
effect, regardless of which application is using it.
Models exist independent of the rest of the system, and are designed to be used by any
application that has access to them. In fact, the database manipulation methods that are avail-
able on model instances can be utilized even from the interactive interpreter, without loading
a Web server or any application- specific logic.
Chapter 3 covers Django models in more detail, including how they’re defined and uti-
lized, how to include your own business logic and much more.
View
While they share a name with the original MVC definition, Django views have little else in
common with the traditional paradigm. Instead, they combine some of the traditional view’s
responsibility with the entirety of the controller’s tasks. A view accepts user input, including
simple requests for information; behaves according to the application’s interaction logic; and
returns a display that is suitable for users to access the data represented by models.
剩余311页未读,继续阅读
mdjgcc
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- GNU gettext 0.16压缩包介绍
- 高级项目风险分析网站:旅游咨询领域的突破
- POD数据挑战:电池存储优化与能源数据分析
- 构建React调色板工具:Dulce React Palette使用教程
- Java实训项目代码解析-34ljc版本4-3
- Dart开发的chiller-app版本控制指南
- Java编程实现最小公倍数的算法实训解析
- mobile-balance:Python库与命令行工具查询移动运营商余额
- Python解决LeetCode分割回文串算法题
- 探索美国手语学习与Jupyter Notebook的应用
- SDV-codes奥迪诺技术解析与应用
- ENV603项目文件与脚本概览
- MATLAB电网模型缩减方法与实例解析
- RGB立方体项目开发:5x5x5灯光效果构建指南
- 陈浩忠Java实验1代码解析
- Tkinter打造Python GUI效率胜过Qt5,节省77.5%文件大小
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功