Writing your first Django app, part 1
编写你的第一个Django程序,第一部分
Let’s learn by example.
从示例程序开始吧。
Throughout this tutorial, we’ll walk you through the creation of a basic poll application.
通过这本新手入门,我们会从头至尾向你介绍怎样创建一个基本的投票程序。
It’ll consist of two parts:
• A public site that lets people view polls and vote in them.
• An admin site that lets you add, change and delete polls.
这个程序包含了两个部分:
一个能够让用户查看投票选项并进行投票的前台。
一个能够进行投票管理的后台。
We’ll assume you have Django installed already. You can tell Django is installed by running the Python interactive interpreter and typing import django. If that command runs successfully, with no errors, Django is installed.
我们假设你已经安装了Django。你可以通过运行Python命令行交互界面并输入import django来确认是否已经安装了Django。如果刚才的命令成功运行了,表示Django已经安装完成。
Where to get help:
If you’re having trouble going through this tutorial, plea post a message to django-urs or drop by #django on irc.freenode to chat with other Django urs who might be able to help.
从哪里获取帮助:
如果你在阅读这篇新手入门的时候遇到任何困难,请在Django的Google小组留言或是访问Django的IRC,从其他的用户那里获得帮助。
Creating a project
创建项目
If this is your first time using Django, you’ll have to take care of some initial tup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of ttings for an instance of Django, including databa configuration, Django-specific options and application-specific ttings.
如果这是你第一次使用Django,你需要注意一下初始化设置。也就是说,你需要自动创建一些Django项目初始化的代码——包括数据库设置、Django特性设置以及程序特性设置。From the command line, cd into a directory where you’d like to store your code, then run the command django-admin.py startproject mysite. This will create a mysite directory in
your current directory.
在命令行界面下,进入到你放置代码的目录中,执行命令django-admin.py startproject mysite。这将在当前目录下创建一个mysite目录。
Mac OS X permissions
谈的成语If you’re using Mac OS X, you may e the message ―permission denied‖ when you try to run djan
go-admin.py startproject. This is becau, on Unix-bad systems like OS X, a file must be marked as ―executable‖ before it can be run as a program. To do this, open Terminal.app and navigate (using the cd command) to the directory where
django-admin.py is installed, then run the command chmod +x django-admin.py.
Mac OS X权限设置
如果你是用的是Mac OS X操作系统,你在执行上述命令的时候可能会看到―permission denied‖的提示信息。这是因为在基于Unix的操作系统中,必须要为文件设置可执行权限才能让文件像程序一样执行。打开终端,切换目录到django-admin.py所在的目录,执行命令chmod +x django-admin.py。
Note
You’ll need to avoid naming projects after built-in Python or Django components. In particular, this means you should avoid using names like django (which will conflict with Django itlf) or test (which conflicts with a built-in Python package).
注意
你要避免用Python内置模块或是Django组件来命名项目。这就是说你应该避免使用django (与Django名称冲突)或是test(与Python内置模块冲突)这样的名称。
django-admin.py should be on your system path if you installed Django via python tup.py. If it’s not on your path, you can find it in site-packages/django/bin, where
`site-packages` is a directory within your Python installation. Consider symlinking to django-admin.py from some place on your path, such as /usr/local/bin.
如果你是通过python tup.py来安装Django,django-admin.py就应该在你的系统路径下。如果不在的话,你可以在site-packages/django/bin下找到,这里site-packages位于Python 的安装路径下。最好在/usr/local/bin下为django-admin.py建立软链接。
Where should this code live?established
If your background is in PHP, you’re probably ud to putting code under the Web rver’s document root (in a place such as /var/www). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your Web rver’s document root, becau it risks the possibility that people may be able to view your code over the Web. That’s not good for curity.
Put your code in some directory outside of the document root, such as /home/mycode.
代码应该放在哪?
如果你有PHP的背景,你很可能习惯把代码放在Web服务器的文档根目录下面(比如
/var/www)。使用Django,你不用这么做。把Python代码放在Web服务器的文档根目录下面并不是一个好办法,因为这会增加用户通过Web服务器浏览你的代码的风险。这样对系统安全性不好。把你的代码放在文档根目录之外,比如/home/mycode。
Let’s look at what startproject created:
我们来看看startproject都创建了些什么:
mysite/
近视防控知识__init__.py
manage.py
ttings.py
urls.py
The files are:
• __init__.py: An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docs if you're a Python beginner.)
• manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin.py and manage.py.
• ttings.py: Settings/configuration for this Django project. Django ttings will tell you all about how ttings work.
药品注册管理办法• urls.py: The URL declarations for this Django project; a "table of contents" of your Django-powered site. You can read more about URLs in URL dispatcher.
这些文件是:
__init__.py 一个空文件,它表示当前文件夹要被当做一个Python包来对待。(如果你是Python初学者,关于包的概念请看官方文档)
manage.py 命令行交互工具。你可以在django-admin.py and manage.py找到与manage.py有关的所有细节资料。
ttings.py Django项目的配置文件。Django设置会告诉你这个文件是怎样工作的。
urls.py Django项目的URL声明文件。你可以在URL分发器中找到更多的相关内容。
The development rver
开发服务器
Let's verify this worked. Change into the mysite directory, if you haven't already, and run the command python manage.py runrver. You'll e the following output on th e command line:
我们来看看这能不能正常运行。进入mysite目录,执行命令python manage.py runrver。
你能看到如下的输出:
0 errors found.
Django version 1.0, using ttings 'ings'
Development rver is running at 127.0.0.1:8000/
Quit the rver with CONTROL-C.
You've started the Django development rver, a lightweight Web rver written purely in Python. We've included this with Django so you can develop things rapidly, without having to deal with configuring a production rver -- such as Apache -- until you're ready for production.
你已经启动了Django开发服务器,一个以Python编写的轻量级Web服务器。我们在Django 中包含了这个小玩意是为了让你在发布程序之前,能够不需要配置生产环境的服务器而进行快速开发。
Now's a good time to note: DON'T u this rver in anything rembling a production environment. It's intended only for u while developing. (We're in the business of making Web frameworks, not Web rvers.)
现在请好好注意:千万别在生产环境上用这个自带的服务器。它只是为开发而使用的。(我们的业务是做Web框架,不是Web服务器。)
大学交换生Now that the rver's running, visit 127.0.0.1:8000/ with your Web browr. You'll e a "Welcome to Django" page, in pleasant, light-blue pastel. It worked!
现在服务器正在运行中,访问127.0.0.1:8000/。你会看到一个有意思的类似蜡笔画的浅蓝色―Welcome to Django‖页面。嗯,它正常运行了!
幼儿语言教案Changing the port
By default, the runrver command starts the development rver on port 8000. If you want to change the rver's port, pass it as a command-line argument. For instance, this command starts the rver on port 8080:
python manage.py runrver 8080
Full docs for the development rver can be found in the runrver reference.
改变端口
默认情况下,runrver让开发服务器通过8000端口进行通信。如果你想改变服务器的端口,把端口号作为命令行参数传入即可。下面的例子就是将服务器端口设置为8080:
python manage.py runrver 8080
有关开发服务器的全部文档,可以在runrver参考中找到。
Databa tup
数据库设置
Now, edit ttings.py. It's a normal Python module with module-level variables reprenting Django ttings. Change the ttings to match your databa's connection parameters:
• DATABASE_ENGINE -- Either 'postgresql_psycopg2', 'mysql' or 'sqlite3'. Other backends are also available.
• DATABASE_NAME -- The name of your databa. If you're using SQLite, the databa will be a file on your computer; in that ca, DATABASE_NAME should be the full absolute path, including filename, of that file. If the file doesn't exist, it will automatically be created when you synchronize the databa for the first time (e below).
When specifying the path, always u forward slashes, even on Windows (e.g.
C:/homes/ur/mysite/sqlite3.db).
• DATABASE_USER -- Your databa urname (not ud for SQLite).
• DATABASE_PASSWORD -- Your databa password (not ud for SQLite).
• DATABASE_HOST -- The host your databa is on. Leave this as an empty string if your databa rver is on the same physical machine (not ud for SQLite).北京空气质量预报
现在编辑ttings.py。这是一个Python的模块文件,有很多代表Django设置的模块变量。你可以改变如下的设置来匹配你的数据库连接参数:
DATABASE_ENGINE postgresql_psycopg2、mysql、sqlite3以及其他的数据库类型都可以。
DATABASE_NAME 数据库的名称。如果你使用SQLite,数据库就是一个文件;这种情况下,DATABASE_NAME应当是这个文件的绝对路径。如果文件不存在,在第一次同步数据库的时候会创建这个文件(下面有详细介绍)。
在填写路径的时候,永远都要使用斜杠符号(/),就算是在Windows上也是如此(比如C:/homes/ur/mysite/sqlite3.db)。
DATABASE_USER 访问数据库的用户名(SQLite下不使用)。
DATABASE_PASSWORD 访问数据库的密码(SQLite下不使用)。
DATABASE_HOST 数据库所在主机。如果你的数据库在同一台物理机器上这里就可以留空(SQLite下不使用)。
If you're new to databas, we recommend simply using SQLite (by tting
姤卦
DATABASE_ENGINE to 'sqlite3'). SQLite is included as part of Python 2.5 and later, so you won't need to install anything el.
如果你是个数据库新手,我们建议你使用SQLite(设置DATABASE_ENGINE为sqlite3)。SQLite包含在Python2.5及其后续版本中,所以你不用再安装其他的任何东西。
Note
If you're using PostgreSQL or MySQL, make sure you've created a databa by this point. Do that with "CREATE DATABASE databa_name;" within your databa's interactive