当前位置: 移动技术网 > IT编程>脚本编程>Python > 开始你的第一个Django应用【Python web实战】

开始你的第一个Django应用【Python web实战】

2020年03月09日  | 移动技术网IT编程  | 我要评论

异蟒狂袭,丰原功补,火影之不死邪帝

在这篇教程中,我们将设置你的数据库,创建你的第一个模型,并快速介绍django的自动生成的管理站点。

ps注意很多人学python过程中会遇到各种烦恼问题,没有人帮答疑容易放弃。为此小编建了个python全栈免费答疑.裙 :七衣衣九起起巴而五(数字的谐音)转换下可以找到了,不懂的问题有老司机解决里面还有最新python教程项目可拿,,一起相互监督共同进步!

数据库配置

现在打开mysite/settings.py这是一个普通的python模块,模块变量表示django的设定。

默认情况下,django配置使用sqlite数据库。如果你是数据库初学者,或者只是感兴趣想随便试试django,这是最简单的选择。sqlite包含在python内,因此你不需要安装任何其他东西来支持你的数据库。所以当你开始你的第一个真实项目时,你会想要使用一个更具扩展性的数据库,比如postgresql,以避免数据库切换引发的麻烦。

如果你希望使用其他数据库,安装合适的数据库绑定并且在databases'default'项目中修改keys以匹配数据库连接设置:

  • engine
    比如 'django.db.backends.sqlite3', 'django.db.backends.postgresql', 'django.db.backends.mysql', or 'django.db.backends.oracle'。还有很多其他后端也有提供。

  • name
    你数据库的名字。如果你使用sqlite,数据库将是你计算机上的一个文件,这种情况下,name应该是这个文件的完整绝对路径,包括文件名。默认值os.path.join(base_dir, 'db.sqlite3')将文件春处在项目目录中。

如果你不使用sqlite作为你的数据库,请务必添加user, password, 以及host。更多详情

写给其他用户的使用者
如果你在使用sqlite之外的数据库,请确保你使用前已经创建了数据库,请在你的数据库里使用create database database_name;

同时确保mysite/settings.py拥有创建数据库的权限。这样就可以自动创建一个在后面的教程中需要的测试数据库。

如果你正在使用sqlite,你不需要提前创建任何东西,数据库文件会在必要的时候自动创建。

当你开始编辑你的mysite/settings.py,设置你的time_zone为你的时区。

另外,注意installed_apps放在文件最上面,它承载着这个django实例中所有活动的django应用的名字,应用可以被使用于多个项目中,你可以打包分发他们让别人在别的项目中使用。

默认情况下,installed_apps随django包含着几个应用。

  • django.contrib.admin管理站点,你能立即使用上。
    -django.contrib.auth认证系统。
    -django.contrib.contenttypes内容类型的框架
    -django.contrib.sessions session框架
    -django.contrib.messages messaging框架
    -django.contrib.staticfiles 静态文件管理

这些应用默认包括在内以方便开发者使用。

其中一些应用程序,需要至少一个数据库表才能使用,因此我们需要在数据库中创建表,然后才能使用。请使用以下的命令:

python manage.py migrate

migrate命令查看installed_apps的设置,并根据mysite/settings.py文件中的数据库设置创建需要的数据库表。如果你感兴趣的话,打开数据库命令行,输入以下命令来看看django帮你创建了什么。

  • \dt(postgresql),
  • show tables;(mysql),
  • .schema(sqlite),
  • select table_name from user_tables;(oracle)

写给极简主义者
正如我们上面所说,我们包含了默认应用为了涵盖常规使用,但并不是每个人都需要它们。如果你不需要,请在运行migrate之前,从installed_apps中随意把注释掉或者删掉对应的代码。migrate命令只会运行installed_apps中的应用。


创建模型

现在,我们使用额外的元数据来定义你的模型(本质上就是你的数据库布局)。

模型的哲学
一个模型是关于您的数据的唯一的确定的来源。它包含着您正在存储的数据的基本字段和行为。django遵从dry原则。我们的目标是在一个地方定义您的数据模型然后自动地从中获取数据。这就包括了迁移在里面。举个例子,不像ruby on rails,django的迁移完全源自您的数据模型,并且本质上只是django可以通过更新数据库模式来匹配当前模型的历史。

在我们这个简单的投票应用中,我们会创建两个模型:问题(question)和选择(choice)。一个问题有一个问题内容和一个发布时间。一个选择有选择的文本和投票计数器。每个选择都会和一个问题关联。

这些概念通过简单的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)

这些代码非常明确。每个模型通过一个继承自django.db.models.model的类来表示。每个模型都有一些类变量,每个变量都表示模型中的数据库字段。

每个字段通过实例中的一个字段类表示,比如charfield是字符字段,datetimefield是时间字段。这会告诉django每个字段保存着什么类型的数据。

每个字段实例的名字(比如question_textpub_date)是字段的对机器友好的格式的名字,你以后可以在你的python代码中使用这个值,你的数据库也会用它作为列名称。

你可以使用(可选的)第一个位置参数作为指定的可读的名称。他可以用于django的几个introspective(内省)部分,同时作为文档。如果没有提供,则django将使用机器可读的名称作为名字。在这个例子中,我们只对question.pub_date定义了一个名字。对于其他所有的字段都会默认使用自己的机器名作为名字。

有些字段类有一些必须的参数要填。举例,charfield要求你给出最大长度max_length。这不仅会用在数据库中,也会在验证中使用,后面我们会讲到。

一个字段也可以拥有很多可选的参数,比如我们可以看到,我们班votes的默认值设成了0。

最后一点,注意关系是由外键定义的。这告诉django,每一个choice都和只和一个问题相关。django支持所有常见的数据库关系:多对一,多对多,一对一。


激活模型(activating models)

这个简单的模型告诉了django很多信息。通过它,django可以做到:

  • 为这个app创建一个数据库对象集合
  • 创建一个python数据库访问api来访问问题和选项的对象。

但是首先,我们应该告诉我们的项目,polls应用已经被安装了。

django哲学小课堂
django的应用是可插拔的。您可以在多个项目中使用某个应用程序,你可以分发应用程序,因为他们并不是和给定的django安装绑定的。

为了把对应的应用包括进我们的项目中,我们需要在installed_apps设置中添加对其配置类的引用。pollsconfig类在polls/app.py文件中,路径是polls.apps.pollsconfig。编辑mysite/settings.py文件,添加路径到installed_apps设置中。

mysite/settings.py
installed_apps = [ 'polls.apps.pollsconfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]

现在django已经知道要把polls应用囊括在内了,下面运行另一条命令:

python manage.py makemigrations polls

你会看到类似下面的结果:

migrations for 'polls': polls/migrations/0001_initial.py: -creat model choice -create model question -add field question to choice

通过运行makemigrations,你可以告诉django你已经对你的model做出了一些改变,并且你希望这些改变可以作为migration存储起来。(在现在的例子里是创建了一个新的model)

django通过migration来存储你的模型(也即是数据库对象)的变化。他们只是硬盘上的一些文件。如果你想,你可以为你的新模型读取一个migration(迁移);也就是polls/migrations/0001_initial.py文件。不用担心,你并不需要每次django作出改变都要读取它们,但是如果你想手动调整django如何改变内容,那么它们是被设计为可以人为编辑的。

有一个命令可以帮你运行migration并且自动管理你的数据库对象,也就是我们稍后会介绍的migrate(迁移)。但是首先让我们看看migration会运行什么样的sql。sqlmigrate命令接受迁移名称,并且返回其sql:

python manage.py sqlmigrate polls 0001

你应该会看到类似下面的结果

begin;
--
-- *create model choice*
--
create table "polls_choice" ( "id" serial not null primary key, "choice_text" varchar(200) not null, "votes" integer not null );
--
-- *create model question*
--
create table "polls_question" ( "id" serial not null primary key, "question_text" varchar(200) not null, "pub_date" timestamp with time zone not null );
--
--*add field question to choice*
--
alter table "polls_choice" add column "question_id" integer not null;
alter table "polls_choice" alter column "question_id" drop default;
create index "polls_choice_7aa0f6ee" on "polls_choice" ("question_id");
alter table "polls_choice" add constraint "polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id" foreign key ("question_id") references "polls_question" ("id") deferrable initially deferred;
commit;

注意:

  • 具体的输出会根据你如何使用的数据库而变化,上面的例子是根据postgresql写的。
  • 表的名字会根据应用的名字以及模型的小写名自动生成(你可以覆盖这个行为)
  • 主键会自动生成(你可以覆盖这个)
  • 按照惯例,django将"_id"附加到外键字段名称。
  • 它根据你使用的数据库量身打造,因此数据库特定的字段类型比如auto_increment(mysql), serial(postgresql)或是integer primary key autoincrement (sqlite) 都会自动帮你处理。字段名的引用也同样如此。
  • sqlmigrate命令并不会真的让你的数据库迁移,只是打印到屏幕上,让你可以看到django需要什么sql。这对于检查django在干什么或者有需要sql脚本更改的数据库管理员非常有用。

如果你感兴趣,你还可以运行python manage.py check,这会不进行任何迁移,不接触数据库来检查你的项目有没有什么问题。

现在,再次运行migrate创建你的模型吧。

`$ python manage.py migrate
operations to perform:
apply all migrations: admin, auth, contenttypes, polls, sessions

running migrations:
rendering model states... done
applying polls.0001_initial... ok`

migrate 命令接收所有未应用的迁移(django通过一个叫做django_migrations的特殊表聊跟踪哪些迁移被应用了)并根据数据库运行他们。本质上是将你对模型的更改与数据库对象进行同步。

迁移非常强大,他可以让你一边建设你的项目,一边慢慢地改变你的数据库,而且不用删除你的数据库或者表,或者创建一个新的。它专门用于实时升级数据库,而不会丢失数据。我们将在本教程的后续部分中更深入地介绍它们,但是现在,请记住进行模型更改的三步指南:

  • 更改你的模型(在 models.py 中)。
  • 运行 python manage.py makemigrations 来创建这些更改的迁移
  • 运行 python manage.py migrate 将这些更改应用于数据库。

django有单独的命令来制作和应用迁移的原因是因为您可以提交迁移到版本控制系统并将其发送到您的应用程序; 它们不仅可以使您的开发更容易,而且还可以被其他开发人员和生产中使用。

请阅读 ,了解有关 manage.py 可以执行的操作的完整信息。


玩转api

现在,我们一起进入交互式的python shell,并使用django提供的免费api。要调用python shell,请使用一下命令:

python manage.py shell

我们没有简单的输入python而是使用这个命令,因为manage.py设置了django_settings_module环境变量,这给了django提供了你的mysite/settings.py文件的python导入路径。

绕过manage.py
如果你不想用manage.py也没问题, 只要将django_settings_module
环境变量设置给mysite.settings,打开一个普通的python shell,然后设置django:
>>>import django
>>>django.setup()

如果这引发了一个attributeerror,你也许用的django版本不是1.11,请更换不同版本的教程或者升级你的django。

你必须在manage.py所在的目录下运行python,或者保证目录在python路径上,import mysite才能正常工作。

更多相关信息,请参考。

进入了shell后,我们来看看database api:

>>> from polls.models import question, choice # import the model classes we just wrote.

# no questions are in the system yet.
>>> question.objects.all()
<queryset []>

# create a new question.
# support for time zones is enabled in the default settings file, so
# django expects a datetime with tzinfo for pub_date. use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
>>> from django.utils import timezone
>>> q = question(question_text="what's new?", pub_date=timezone.now())

# save the object into the database. you have to call save() explicitly.
>>> q.save()

# now it has an id. note that this might say "1l" instead of "1", depending
# on which database you're using. that's no biggie; it just means your
# database backend prefers to return integers as python long integer
# objects.
>>> q.id
1

# access model field values via python attributes.
>>> q.question_text
"what's new?"
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<utc>)

# change values by changing the attributes, then calling save().
>>> q.question_text = "what's up?"
>>> q.save()

# objects.all() displays all the questions in the database.
>>> question.objects.all()
<queryset [<question: question object>]>

等一下!<>完全是一个没有任何帮助的表达,让我们编辑问题模型(在polls/models.py文件中)来解决这个问题。然后添加__str__()方法到question和choice。

polls/models.py
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible # only if you need to support python 2
class question(models.model): def __str__(self): return self.question_text

@python_2_unicode_compatible # only if you need to support python 2
class choice(models.model): def __str__(self): return self.choice_text

__str__()方法的添加是很重要的,不光是为了你自己处理交互提示的方便,也是因为对象的展示是通过django的自动生成admin的。

注意这些都是普通python方法,现在我们添加一个自定义方法,为了演示使用。

polls/models.py
import datetime

from django.db import models
from django.utils import timezone

class question(models.model): def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

请注意,添加import datetime和django.utils import timezone可以分别在django.utils.timezone中引用python的标准datetime模块和django的时区相关实用程序。如果您不熟悉python中的时区处理,您可以在.中了解更多信息。

保存这些更改并通过再次运行python manage.py shell启动一个新的python shell:

>>> from polls.models import question, choice

# make sure our __str__() addition worked.
>>> question.objects.all()
<queryset [<question: what's up?>]>

# django provides a rich database lookup api that's entirely driven by
# keyword arguments.
>>> question.objects.filter(id=1)
<queryset [<question: what's up?>]>
>>> question.objects.filter(question_text__startswith='what')
<queryset [<question: what's up?>]>

# get the question that was published this year.
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> question.objects.get(pub_date__year=current_year)
<question: what's up?>

# request an id that doesn't exist, this will raise an exception.
>>> question.objects.get(id=2)
traceback (most recent call last):
...
doesnotexist: question matching query does not exist.

# lookup by a primary key is the most common case, so django provides a
# shortcut for primary-key exact lookups.
# the following is identical to question.objects.get(id=1).
>>> question.objects.get(pk=1)
<question: what's up?>

# make sure our custom method worked.
>>> q = question.objects.get(pk=1)
>>> q.was_published_recently()
true

# give the question a couple of choices. the create call constructs a new
# choice object, does the insert statement, adds the choice to the set
# of available choices and returns the new choice object. django creates
# a set to hold the "other side" of a foreignkey relation
# (e.g. a question's choice) which can be accessed via the api.
>>> q = question.objects.get(pk=1)

# display any choices from the related object set -- none so far.
>>> q.choice_set.all()
<queryset []>

# create three choices.
>>> q.choice_set.create(choice_text='not much', votes=0)
<choice: not much>
>>> q.choice_set.create(choice_text='the sky', votes=0)
<choice: the sky>
>>> c = q.choice_set.create(choice_text='just hacking again', votes=0)

# choice objects have api access to their related question objects.
>>> c.question
<question: what's up?>

# and vice versa: question objects get access to choice objects.
>>> q.choice_set.all()
<queryset [<choice: not much>, <choice: the sky>, <choice: just hacking again>]>
>>> q.choice_set.count()
3

# the api automatically follows relationships as far as you need.
# use double underscores to separate relationships.
# this works as many levels deep as you want; there's no limit.
# find all choices for any question whose pub_date is in this year
# (reusing the 'current_year' variable we created above).
>>> choice.objects.filter(question__pub_date__year=current_year)
<queryset [<choice: not much>, <choice: the sky>, <choice: just hacking again>]>

# let's delete one of the choices. use delete() for that.
>>> c = q.choice_set.filter(choice_text__startswith='just hacking')
>>> c.delete()

有关模型关系的更多信息,请参阅访问 . 有关如何使用双下划线通过api执行字段查找的更多信息,请参阅 field lookups. 有关数据库api的完整详细信息,请参阅我们的 database api reference.


django admin介绍

"django的哲学"小课堂
django管理站点是为你的员工或者客户的简单的添加,更改,删除这些繁琐工作而设计的。django管理站点会完全自动生成模型的管理站。

django当年创建于新闻编辑室,为内容管理而生的,“内容发布者”和“公共”网站之间的界限非常明确。网站管理人员使用该系统添加新闻故事,事件,体育比分等,该内容显示在公共站点上。 django解决了为站点管理员创建统一界面来编辑内容的问题。

这个管理站点不打算由网站访问者使用。这是网站经理。

创建一个管理员用户

首先,我们需要创建一个用户来登录管理站点。使用下面的命令:
python manage.py createsuperuser
输入你想要的用户名
username: admin
输入邮箱地址
email address: admin@example.com
最后输入你的密码两次。
password: **********
password (again): *********
superuser created successfully.

开启你的开发服务器

django管理站点会自动启动,我们只要启动开发服务器就可以来一起探索它了。
使用python manage.py runserver开启服务器。
现在打开一个浏览器,并且去/admin/-例如http://127.0.0.1:8000/admin/你应该会看到django administration的登录界面。

进入admin管理站点。
总结:很多人学python过程中会遇到各种烦恼问题,没有人帮答疑容易放弃。为此小编建了个python全栈免费答疑.裙 :七衣衣九起起巴而五(数字的谐音)转换下可以找到了,不懂的问题有老司机解决里面还有最新python教程项目可拿,,一起相互监督共同进步!

本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网