Django 1.10中文文档:第一个应用 part 2

本教程将继续上一部分,介绍如何配置数据库、创建第一个模型,以及初步接触Django的后台管理系统。我们将学习模型的创建、激活、API使用,以及Django Admin的基本操作,包括创建管理员、启动开发服务器、在Admin中注册应用及探索其功能。
摘要由CSDN通过智能技术生成

翻译文档传送门

gitbook地址

https://run-noob.gitbooks.io/django-chinese-docs-1-10/content/

This tutorial begins where Tutorial 1 left off. We’ll setup the database, create your first model, and get a quick introduction to Django’s automatically-generated admin site.

紧接着教程第一部分,我们开始配置数据库,创建你的第一个model,并且快速了解下Django的后台管理系统

Database setup

Now, open up mysite/settings.py. It’s a normal Python module with module-level variables representing Django settings.

现在,打开 mysite/settings.py. 这是一个普通的python模块包,包含了Django配置的模块级变量。

By default, the configuration uses SQLite. If you’re new to databases, or you’re just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won’t need to install anything else to support your database. When starting your first real project, however, you may want to use a more scalable database like PostgreSQL, to avoid database-switching headaches down the road.

默认情况下,Django使用的是SQLite数据库。如果你是数据库菜鸟或者你仅仅想试用一下Django,这是最简单的选择了。python自带了SQLite模块,所以你不需要安装任何东西。然后当你真正想写一个项目时,你可能需要用到功能更丰富的数据库,比如说PostgreSQL,避免后面面临数据库切换的头疼问题。

If you wish to use another database, install the appropriate database bindings and change the following keys in theDATABASES ‘default’ item to match your database connection settings:

如果你想用其他的数据库,请先安装好相应的数据库软件 ,并修改settings文件中DATABASES ‘default’ 选项,用于连接你的数据库:

  • ENGINE – Either ‘django.db.backends.sqlite3’
    , ‘django.db.backends.postgresql’,’django.db.backends.mysql’, or ‘django.db.backends.oracle’. Other backends are also available.

  • 可以是’django.db.backends.sqlite3’或者’django.db.backends.postgresql’,’django.db.backends.mysql’, or ’django.db.backends.oracle’,当然 其它可用的也行。

  • NAME – The name of your database. If you’re using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. The default value, os.path.join(BASE_DIR, ‘db.sqlite3’), will store the file in your project directory.

  • 数据库的名字。如果你使用的是默认的SQLite,那么数据库将作为一个文件将存放在你的本地机器内,NAME应该是这个文件的完整绝对路径,包括文件名。设置中的默认值os.path.join(BASE_DIR, ’db.sqlite3’),将把该文件储存在你的项目目录下。

If you are not using SQLite as your database, additional settings such as USER, PASSWORD, and HOST must be added. For more details, see the reference documentation for DATABASES.

如果你不是用SQLite,那你需要配置其他的设置,如USER, PASSWORD, and HOST 都是必需的. 详细请看DATABASES相关.

For databases other than SQLite
If you’re using a database besides SQLite, make sure you’ve created a database by this point. Do that with “CREATE DATABASE database_name;” within your database’s interactive prompt.
Also make sure that the database user provided in mysite/settings.py
has “create database” privileges. This allows automatic creation of a test database which will be needed in a later tutorial.
If you’re using SQLite, you don’t need to create anything beforehand - the database file will be created automatically when it is needed.

**SQLite外的其他数据库
在使用非SQLite的数据库时,请务必首先在数据库提示符交互模式下创建数据库,你可以使用命令:“CREATE DATABASE database_name;”
确保你在settings文件中提供的数据库用户具有创建数据库表的权限,因为在接下来的教程中,我们需要自动创建一个test数据库。
如果你使用的是SQLite,那么你无需做任何预先配置,数据库文件会自动创建

While you’re editing mysite/settings.py, set TIME_ZONE to your time zone.

Also, note the INSTALLED_APPS setting at the top of the file. That holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects.

By default, INSTALLED_APPS contains the following apps, all of which come with Django:

  • django.contrib.admin – The admin site. You’ll use it shortly.
  • django.contrib.auth – An authentication system.
  • django.contrib.contenttypes – A framework for content types.
  • django.contrib.sessions – A session framework.
  • django.contrib.messages – A messaging framework.
  • django.contrib.staticfiles – A framework for managing static files.

These applications are included by default as a convenience for the common case.

Some of these applications make use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:

在修改settings文件时,请顺便将TIME_ZONE设置为你所在的时区。

同时,请注意settings文件中顶部的INSTALLED_APPS设置项。它保存了所有的在当前项目中被激活的Django应用。你必须将你自定义的app注册在这里。每个应用可以被多个项目使用,而且你可以打包和分发给其他人在他们的项目中使用。

默认情况,INSTALLED_APPS中包含以下应用,它们都是Django自带的:

  • django.contrib.admin:admin站点
  • django.contrib.auth:身份认证系统
  • django.contrib.contenttypes:内容类型框架
  • django.contrib.sessions:会话框架
  • django.contrib.messages:消息框架
  • django.contrib.staticfiles:静态文件管理框架

以上应用默认会包含,对于大多数项目都是需要的

上面的每个应用都至少需要使用一个数据库表,所以在使用它们之前我们需要在数据库中创建这些表。使用这个命令:

python manage.py migrate

The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file and the database migrations shipped with the app (we’ll cover those later). You’ll see a message for each migration it applies. If you’re interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MySQL), .schema (SQLite), or SELECT TABLE_NAME FROM USER_TABLES; (Oracle) to display the tables Django created.

migrate命令将根据INSTALLED_APPS、mysite/settings.py的数据库设置以及app的数据库迁移(稍后会讲到)信息创建相关的表 ,你讲看到每一个数据库消息的信息。如果你感兴趣,可以在你的数据库命令行下输入:\dt (PostgreSQL), SHOW TABLES; (MySQL), 或 .schema (SQLite), 或SELECT TABLE_NAME FROM USER_TABLES; (Oracle) 来列出 Django 所创建的表。

For the minimalists

Like we said above, the default applications are included for the common case, but not everybody needs them. If you don’t need any or all of them, feel free to comment-out or delete the appropriate line(s) from INSTALLED_APPS before running migrate. The migrate command will only run migrations for apps in INSTALLED_APPS.

就像我们上面说的,默认情况下会自带那些公共应用。如果你不需要其中一个或所有的,你可以在migrate前,将INSTALLED_APPS内注释掉或者删除对应的行。 migrate命令只会针对INSTALL_APPS进行操作

Creating models

Now we’ll define your models – essentially, your database layout, with additional metadata.

Philosophy

A model is the single, definitive source of truth about your data. It contains the essential fields and behaviors of the data you’re storing. Django follows the DRY Principle. The goal is to define your data model in one place and automatically derive things from it.

This includes the migrations - unlike in Ruby On Rails, for example, migrations are entirely derived from your models file, and are essentially just a history that Django can roll through to update your database schema to match your current models.

创建models

现在我们开始定义models - 本质上,就是你的数据库结构,和其他的一些元数据信息

Philosophy

model就是你唯一可信的真实数据源。它包含了你所要存储数据的必须字段和行为信息。Django遵循DRY Principle,目的是让你可以在一个地方定义你的数据模型,并以它来驱动整个项目

关于migrations - 它不想ruby的Rails,比如说,migrations纯靠你的models来驱动。本质上是根据当前models来更新数据库表的一个历史记录。

In our simple poll app, we’ll create two models: Question and Choice. A Question has a question and a publication date. A Choice has two fields: the text of the choice and a vote tally. Each Choice is associated with a Question.

These concepts are represented by simple Python classes. Edit the polls/models.py file so it looks like this:

在我们的投票应用中,我们将创建两个模型:Question and Choice。Question表包含问题和发布日期两个字段。Choice表有两个字段:选择文本和投票计数。每一条choice都关联到一条Question

这些概念都由python的类来定义。编辑polls/models.py,如下

polls/models.py

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

The code is strai

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值