Django 简明教程
Django - Creating a Project
既然我们已经安装了 Django,我们开始使用它。在 Django 中,你想要创建的每个 Web 应用程序都被称为一个项目;而一个项目是应用程序的总和。应用程序是依赖于 MVT 模式的一组代码文件。举个例子,假设我们要构建一个网站,网站是我们的项目,论坛、新闻、联系引擎是应用程序。这种结构使得在项目之间移动应用程序变得更容易,因为每个应用程序都是独立的。
Now that we have installed Django, let’s start using it. In Django, every web app you want to create is called a project; and a project is a sum of applications. An application is a set of code files relying on the MVT pattern. As example let’s say we want to build a website, the website is our project and, the forum, news, contact engine are applications. This structure makes it easier to move an application between projects since every application is independent.
Create a Project
无论你在 Windows 还是 Linux 上,只需打开一个终端或一个 cmd 提示符,然后导航到想要创建项目的位置,然后使用以下代码 −
Whether you are on Windows or Linux, just get a terminal or a cmd prompt and navigate to the place you want your project to be created, then use this code −
$ django-admin startproject myproject
这将创建一个具有以下结构的 “myproject” 文件夹 −
This will create a "myproject" folder with the following structure −
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
The Project Structure
“myproject” 文件夹只是你的项目容器,它实际上包含两个元素 −
The “myproject” folder is just your project container, it actually contains two elements −
-
manage.py − This file is kind of your project local django-admin for interacting with your project via command line (start the development server, sync db…). To get a full list of command accessible via manage.py you can use the code −
$ python manage.py help
-
The “myproject” subfolder − This folder is the actual python package of your project. It contains four files − init.py − Just for python, treat this folder as package. settings.py − As the name indicates, your project settings. urls.py − All links of your project and the function to call. A kind of ToC of your project. wsgi.py − If you need to deploy your project over WSGI.
Setting Up Your Project
你的项目在子文件夹 myproject/settings.py
中设置。下面是你可能需要设置的一些重要选项 −
Your project is set up in the subfolder myproject/settings.py. Following are some important options you might need to set −
DEBUG = True
此选项允许你设置你的项目是否处于调试模式。调试模式让你了解你的项目的错误。切勿为实时项目将其设置为 “True”。但是,如果你希望 Django 灯光服务器提供静态文件,则必须将其设置为 “True”。仅在开发模式下执行此操作。
This option lets you set if your project is in debug mode or not. Debug mode lets you get more information about your project’s error. Never set it to ‘True’ for a live project. However, this has to be set to ‘True’ if you want the Django light server to serve static files. Do it only in the development mode.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'database.sql',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
数据库在 “Database” 词典中设置。上面的示例适用于 SQLite 引擎。如前所述,Django 还支持 −
Database is set in the ‘Database’ dictionary. The example above is for SQLite engine. As stated earlier, Django also supports −
-
MySQL (django.db.backends.mysql)
-
PostGreSQL (django.db.backends.postgresql_psycopg2)
-
Oracle (django.db.backends.oracle) and NoSQL DB
-
MongoDB (django_mongodb_engine)
在设置任何新引擎之前,请确保你安装了正确的数据库驱动程序。
Before setting any new engine, make sure you have the correct db driver installed.
你还可以设置其他选项,例如:TIME_ZONE
、LANGUAGE_CODE
、TEMPLATE
……
You can also set others options like: TIME_ZONE, LANGUAGE_CODE, TEMPLATE…
现在你的项目已经创建并配置,确保它正常工作 −
Now that your project is created and configured make sure it’s working −
$ python manage.py runserver
运行以上代码后,你会看到类似以下内容 −
You will get something like the following on running the above code −
Validating models...
0 errors found
September 03, 2015 - 11:41:50
Django version 1.6.11, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.