创建应用程序
```python
learning_log$ python manage.py startapp
(ll_env) PS C:\Users\Administrator\learning_log> python manage.py startapp learning_logs
(ll_env) PS C:\Users\Administrator\learning_log> dir
目录: C:\Users\Administrator\learning_log
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2019/9/7 23:00 learning_log
d----- 2019/9/8 13:28 learning_logs
d----- 2019/9/7 20:31 ll_env
-a---- 2019/9/7 23:13 131072 db.sqlite3
-a---- 2019/9/7 22:12 832 manage.py
(ll_env) PS C:\Users\Administrator\learning_log> dir learning_logs
目录: C:\Users\Administrator\learning_log\learning_logs
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2019/9/8 13:28 migrations
-a---- 2019/9/8 13:28 66 admin.py
-a---- 2019/9/8 13:28 105 apps.py
-a---- 2019/9/8 13:28 60 models.py
-a---- 2019/9/8 13:28 63 tests.py
-a---- 2019/9/8 13:28 66 views.py
-a---- 2019/9/8 13:28 0 __init__.py
admin.py :管理站点模型的声明文件,默认为空。
apps.py :应用信息定义文件。在其中生成了类Appconfig,类用于定义应用名等Meta数据。
migrations : 用于在之后定义引用迁移功能。
models.py : 添加模型层数据类的文件。
test.py :测试代码文件。
views.py :定义URL响应函数。
新增了learning_logs文件,其中有
1、定义模型
打开models.py
from django.db import models
# Create your models here.
class Topic(models.Model):
# 继承Model,Django中一个定义模型基本功能的类。
# Topic两个属性
# Text由字符或文本组成的数据,存储少量文本,如名称、标题
# date_added记录日期和时间的数据
"""用户学习的主题"""
text = models.CharField(max_length=200)
# 预留200个字符的空间
date_added = models.DateTimeField(auto_now_add=True)
# 设置为当前时间
def __str__(self):
"""返回模型的字符串表示"""
return self.text
2、激活模型
要使用模型,需让Django将应用程序包含到项目中,setting.py中的添加前面程序到INSTALLED_APPS的元组中。
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My app
'learning_logs',
]
需要让Django修改数据库,对learning_logs调用makemigrations,命令makemigrations让Django确定该如何修改数据库,使其能够存储于模型Topic有关的信息,创建了一个名为0001_initial.py的迁移文件,在数据库中为Topic创建一个表。
(ll_env)learning_log$ python manage.py makemigrations learning_logs
让Django迁移项目,
(ll_env)learning_log$ python manage.py migrate
每当修改“学习笔记”管理的数据是,都应采取三个步骤:
1.修改models.py
2.对learning_logs调用makemigrations
3.让Django迁移项目**
3、Django管理网站
3.1 创建超级用户
(ll_env) PS C:\Users\Administrator\learning_log> python manage.py createsuperuser
Username (leave blank to use 'administrator'): ll_admin
Email address: wangyi810@126.com
Password:
Password (again):
Superuser created successfully.
(ll_env) PS C:\Users\Administrator\learning_log>
Django并不存储输入的密码,存储从密码派生出来的一个字符串——散列值
3.2 向管理网站注册模型
C:\Users\Administrator\learning_log\learning_logs下的admin.py
from django.contrib import admin
# Register your models here.
from learning_logs.models import Topic
# 从learning_logs文件夹中,导入models.py,导入Topic模型
admin.site.register(Topic)
# 让Django通过管理网站管理模型
(ll_env) PS C:\Users\Administrator\learning_log> python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
September 10, 2019 - 21:03:22
Django version 1.11, using settings 'learning_log.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[10/Sep/2019 21:03:31] "GET / HTTP/1.1" 200 1716
Not Found: /favicon.ico
[10/Sep/2019 21:03:31] "GET /favicon.ico HTTP/1.1" 404 1968
[10/Sep/2019 21:03:51] "GET /admin/ HTTP/1.1" 302 0
[10/Sep/2019 21:03:51] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1650
[10/Sep/2019 21:03:51] "GET /static/admin/css/base.css HTTP/1.1" 200 16066
[10/Sep/2019 21:03:51] "GET /static/admin/css/login.css HTTP/1.1" 200 1203
[10/Sep/2019 21:03:51] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
Not Found: /favicon.ico
[10/Sep/2019 21:03:52] "GET /favicon.ico HTTP/1.1" 404 1968
[10/Sep/2019 21:03:52] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[10/Sep/2019 21:03:52] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
访问:http://localhost:8000/admin/
3.3 添加主题
可以添加主题
4、定义模型Entry
C:\Users\Administrator\learning_log\learning_logs\models.py
from django.db import models
# Create your models here.
class Topic(models.Model):
# 继承Model,Django中一个定义模型基本功能的类。
# Topic两个属性
# Text
"""用户学习的主题"""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""返回模型的字符串表示"""
return self.text
class Entry(models.Model):
"""学到关于某个主题的具体知识"""
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
# 删除关联数据,与之关联也删除,ForeignKey外键
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
"""返回模型的字符串表示"""
return self.text[:50]+"..."
ForeignKey没看明白
https://blog.csdn.net/lvze0321/article/details/53321736
https://blog.csdn.net/qq_33867131/article/details/82591821
留两个贴研究研究
5、迁移模型Entry
(ll_env) PS C:\Users\Administrator\learning_log> python manage.py makemigrations learning_logs
Migrations for 'learning_logs':
learning_logs\migrations\0002_entry.py
- Create model Entry
(ll_env) PS C:\Users\Administrator\learning_log> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, learning_logs, sessions
Running migrations:
Applying learning_logs.0002_entry... OK
6、向管理网站注册Entry
C:\Users\Administrator\learning_log\learning_logs\admin.py
from django.contrib import admin
# Register your models here.
from learning_logs.models import Topic, Entry
# 从learning_logs文件夹中,导入models.py,导入Topic模型
admin.site.register(Topic)
# 让Django通过管理网站管理模型Topic
# 为了让 admin 界面管理某个数据模型,我们需要先注册该数据模型到 admin
admin.site.register(Entry)
# 让Django通过管理网站管理模型Entry
# admin详解
# https://www.runoob.com/django/django-admin-manage-tool.html
(ll_env) PS C:\Users\Administrator\learning_log> python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
September 10, 2019 - 21:55:16
Django version 1.11, using settings 'learning_log.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
7、Django shell
交互式终端会话,以编程的方式查看这些数据,是测试项目和排除其估值的理想之地。
(ll_env) PS C:\Users\Administrator\learning_log> python manage.py shell
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from learning_logs.models import Topic
>>> Topic.objects.all() #查询集
<QuerySet [<Topic: Chess>, <Topic: Climbing>]>
# 查看分配给每个主题对象的ID
>>> Topic.objects.all()
<QuerySet [<Topic: Chess>, <Topic: Climbing>]>
>>> topics = Topic.objects.all()
>>> for topic in topics:
... print(topic.id, topic)
File "<console>", line 2
print(topic.id, topic)
^
IndentationError: expected an indented block
>>> for topic in topics:
... print(topic.id, topic)
...
1 Chess
2 Climbing
>>>
#获取对象Chess的属性text和date_added的值
>>> t = Topic.objects.get(id=1)
>>> t.text
'Chess'
>>> t.date_added
datetime.datetime(2019, 9, 10, 13, 7, 59, 265790, tzinfo=<UTC>)
# 由于给模型Entry定义了属性topic,这是一个ForeignKey,将条目与主题关联起来,利用这种关系
# Django能够获取与特定主题相关联的所有条目,如下所示:
>>> t.entry_set.all()
<QuerySet [<Entry: 国际象棋的介绍,我不想写了后面用1235134567234562345612345671234567...>]>