极简主义
django (72), Python(59)(这是由Kenneth Arnold编写的多件系列中的第一个,看着使用Django,少有时兴起它的开销。我对这个系列兴奋,并与Ken一起使用。现在,没有进一步ADO,到内容。 - Will)
对于我们大多数人来说,一把剑是一个带有奇怪手柄的重金属棒。我甚至无法用一个灌木。但是由熟练的战士挥舞着,一把剑是一种强大的武器。
Likewise, Django (and even Python!) can seem big and clunky. But in skilled hands, it's agile enough to level the shrubs of web development while strong enough to take on larger foes as well.
这个迷你系列的目标是帮助你挥舞Django来做你的愿望。我们将以最小的最低限度和从那里积聚。一路上,我们将采取一些非传统的转弯,以在Ajaxy“Web 2.0”应用程序中建立您的敏捷性。
如果您之前使用过Django,则应该能够轻松浏览前两个文章。回来加入我们的#3,我将在哪里介绍django-webapp.。继续检查一下;评论赞赏。
基本
- 我们将使用Django SVN(“开发版”)。根据这套设置设置
指示。 - I develop using Python 2.5, but it should work on Python 2.4 without modification. Python 2.3 will require a few more changes (particularly, decorators).
- 采用iPython.。
- 记住:django是python.。
最小的django
这Django教程通过广泛的概览开始,这适合向您展示您在一个大项目中使用它的方式。如果您首先通过获得大型图片学会最佳的人,您可能希望在此之前通过教程工作。而不是从大图片开始,我们首先练习一些非常基本的动作。
在一个空目录中启动,只需以下内容app.py.
。
qaodmasdkwaspemas0ajkqlsmdqpakldnzsdfl.
你可能会猜到这是什么,甚至没有运行它。让我们无论如何都要运行它。提出一个Python控制台 - 或者更好,Ipython - 并告诉它:
qaodmasdkwaspemas1ajkqlsmdqpakldnzsdfls
Don't worry about the details of this last part; Django will give us a way to automate that. You can just notice that Django can useWSGI.,当您想要在大型服务器上部署您的应用程序时,这将是有趣的。稍后来。
转到http://127.0.0.1/由您的第一个(或至少,您的最小)Django应用程序接待。现在到了解释。
Django的主要工作是将请求(browser) into responses. The most important part of a request is the URL, so that's the first thing we tell Django about. The line marked (1) in the shell code told Django that yourroot_urlconf.
-- the place to start looking up what to do with a URL -- is the应用程序
module that you just made. (Note that just likeimport app
in plain Python, it doesn't end in。py
。)
URL配置
A Django URL configuration module, like the one you just made called应用程序
,只需要一件事:一个调用的变量URLPatterns.
(上面的第(2))。它对请求该URL时运行的函数对其进行对。当您从服务器请求URL时,URL解析程序按顺序浏览列表,寻找该URL的匹配项。
好的,这略有谎言。Web服务器很少处理完整的URL,Django也不例外。解析器试图匹配的URL是小路请求。这就是地址栏中的网址,减去了http://
和服务器名称,减去任何得到
parameters (we'll get back to that). So if you put in:
http://myserver.com/posts/?all=0.
然后你的网址就是posts/
。(YUP,也没有初始斜线。)
URL由正则表达式(正则表达式)匹配。如果你从未听说过他们,不要恐慌;他们是一个非常有力的方式来处理字符串,值得为此而值得学习,但我们不会在这里使用几乎他们的全部权力。我们使用的正则表达式匹配开始的字符串(^
) is immediately followed by the end ($
),即空字符串。所以当我们要求的时候它匹配http://127.0.0.1/
。当它匹配时,它会运行我们传递给它的视图功能。(函数也是物体; Python的酷。)
你可能会发现Python Re模块的文档helpful, particularly the语法文档。
意见
视图将请求转换为响应。他们总是如他们的第一个参数,一个httprequest对象,每个人都喜欢打电话request
。(URL配置也可以将一些其他参数传递给视图。我们会回到那个。)我们定义了我们的视图,称为greet
,在上面标有(3)的线上。
这single job of a view is to return anhttpresponse.
object. It's basically a string, but can hold extra headers, response codes, etc. In line (4), we just construct the response from a constant string.
那就是它!这里没有魔法。用这个和几个位的Django文档武装,你可以轻松地做到你在PHP中做的任何东西。但留下来调整;Django可以让你做更多......
Postscript for the screaming Django experts
Yes, I'm starting out with some very non-idiomatic Django. I wanted to start with the bare minimum of Django's abstractions; once you're used to them, switching to more complex things likesettings.py.
那图案
, 独立views.py
等应该是一个简单的一步。我们会尽快采取一些这些步骤。
另外,我想使用URL.
直接,正如我在这里,更明确而不是图案
。但超过一个偏好的问题,它实际上将在以后的分期付款中更清楚一些事情。如果你认为这是一个坏主意,请告诉我为什么,我很乐意改变匹配的惯例。