18.1 建立项目
建立项目时,首先要以规范的方式对项目进行描述,再建立虚拟环境,以便在其中创建项目
1.1 制定规范
完整的规范详细说明了项目的目标,功能,外观和用户界面。该项目规范如下:
我们要编写一个名为‘学习笔记’的web应用程序,让用户能记录感兴趣的主题,并在学习每个主题的过程中添加日志条目。‘学习笔记’的主页对这个网站进行描述,并邀请用户注册或登录。用户登录后,就可以创建新主题、添加新条目以及阅读既有的条目。
1.2 建立虚拟环境
使用Django,首先建立一个虚拟环境。这对以后将‘学习笔记’部署到服务器上也是必须的。
1.3 安装 virtualenv
1.4 激活虚拟环境
l@ubuntu:~/Documents/lyb$ source bin/activate #激活命令 (lyb) l@ubuntu:~/Documents/lyb$ #激活状态 deactivate #退出激活状态
1.5 安装Django
激活状态下:
pip install Django
1.6 在Django中创建项目
1 (lyb) l@ubuntu:~/Documents$ django-admin.py startproject learning_log . 2 (lyb) l@ubuntu:~/Documents$ ls 3 learning_log lyb manage.py 4 (lyb) l@ubuntu:~/Documents$ ls learning_log 5 __init__.py settings.py urls.py wsgi.py 6 (lyb) l@ubuntu:~/Documents$
1.7 创建数据库
1 (lyb) l@ubuntu:~/Documents$ python manage.py migrate 2 Operations to perform: 3 Apply all migrations: admin, auth, contenttypes, sessions 4 Running migrations: 5 Applying contenttypes.0001_initial... OK 6 Applying auth.0001_initial... OK 7 Applying admin.0001_initial... OK 8 Applying admin.0002_logentry_remove_auto_add... OK 9 Applying contenttypes.0002_remove_content_type_name... OK 10 Applying auth.0002_alter_permission_name_max_length... OK 11 Applying auth.0003_alter_user_email_max_length... OK 12 Applying auth.0004_alter_user_username_opts... OK 13 Applying auth.0005_alter_user_last_login_null... OK 14 Applying auth.0006_require_contenttypes_0002... OK 15 Applying auth.0007_alter_validators_add_error_messages... OK 16 Applying auth.0008_alter_user_username_max_length... OK 17 Applying sessions.0001_initial... OK 18 (lyb) l@ubuntu:~/Documents$ ls 19 db.sqlite3 learning_log lyb manage.py
1.8 查看项目
1 (lyb) l@ubuntu:~/Documents$ python manage.py runserver 2 Performing system checks... 3 4 System check identified no issues (0 silenced). 5 November 04, 2017 - 16:13:50 6 Django version 1.11.7, using settings 'learning_log.settings' 7 Starting development server at http://127.0.0.1:8000/ 8 Quit the server with CONTROL-C.
18.2 创建应用程序
重新打开一个终端:
1 l@ubuntu:~/Documents$ source lyb/bin/activate 2 (lyb) l@ubuntu:~/Documents$ python manage.py startapp learning_logs 3 (lyb) l@ubuntu:~/Documents$ ls 4 db.sqlite3 learning_log learning_logs lyb manage.py 5 (lyb) l@ubuntu:~/Documents$ ls learning_logs/ 6 admin.py apps.py __init__.py migrations models.py tests.py views.py 7 (lyb) l@ubuntu:~/Documents$
2.1 定义模型
打开models.py,输入模型:
from django.db import models # Create your models here. class Topic(models.Model): '''用户学习的主题''' text = models.CharField(max_length=200) date_added = models.DateTimeField(auto_now_add=True) def __str__(self): '返回模型的字符串表示 return self.text
2.2 激活模型
打开/home/l/Documents/learning_log中的setting.py,在INSTALLED_APPS中添加 ‘learning_logs’
继续命令行:
(lyb) l@ubuntu:~/Documents$ python manage.py makemigrations learning_logs Migrations for 'learning_logs': learning_logs/migrations/0001_initial.py - Create model Topic (lyb) l@ubuntu:~/Documents$ python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, learning_logs, sessions Running migrations: Applying learning_logs.0001_initial... OK
每当需要修改‘学习笔记’管理的数据时,都采取如下三个步骤:修改models.py;对learning_logs调用makemigrations;让Django迁移项目。
2.3 Django管理网站
1 创建超级用户
(lyb) l@ubuntu:~/Documents$ python manage.py createsuperuser Username (leave blank to use 'l'): lyb Email address: Password: Password (again): This password is too short. It must contain at least 8 characters. Password: Password (again): Superuser created successfully.
2. 向管理网站注册模型
打开models.py所在目录中的admin.py,输入:
from django.contrib import admin
# Register your models here.
from learning_logs.models import Topic
admin.site.register(Topic)
打开浏览器访问http://localhost:8000/admin/,输入超级用户名及密码后你会看到:
3. 添加主题
2.4 定义模型Entry
models.py
class Entry(models.Model): '''学到的有关某个主题的具体知识''' topic = models.ForeignKey(Topic) text = models.TextField() date_added = models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural = 'entries' def __str__(self): '返回模型的字符串表示' return self.text[:50] + '...'
2.5 迁移模型Entry
(lyb) l@ubuntu:~/Documents$ python manage.py makemigrations learning_logs Migrations for 'learning_logs': learning_logs/migrations/0002_entry.py - Create model Entry (lyb) l@ubuntu:~/Documents$ python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, learning_logs, sessions Running migrations: Applying learning_logs.0002_entry... OK
2.6 向管理网站注册Entry
admin.py
from django.contrib import admin
# Register your models here.
from learning_logs.models import Topic,Entry
admin.site.register(Topic)
admin.site.register(Entry)
2.7 Django shell
1 (lyb) l@ubuntu:~/Documents$ python manage.py shell 2 Python 3.5.2 (default, Sep 14 2017, 22:51:06) 3 [GCC 5.4.0 20160609] on linux 4 Type "help", "copyright", "credits" or "license" for more information. 5 (InteractiveConsole) 6 >>> from learning_logs.models import Topic 7 >>> Topic.objects.all() 8 <QuerySet [<Topic: chess>, <Topic: Rock Clibing>]> # QuerySet 查询集