Django app2
前言
这部分将使用数据库,Django 默认的是sqlite3,如果需要修改数据库的配置,在创建的Django的项目下的settings.py文件下修改配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
# 'django.db.backends.postgresql',
# 'django.db.backends.mysql',
# 'django.db.backends.oracle'
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
修改ENGINE的内容,如 ‘django.db.backends.postgresql’, ‘django.db.backends.mysql’, or ‘django.db.backends.oracle’
sqlite3是Django内置的,操作简单
migrate
py manage.py migrate
migrate命令查看已安装的应用程序设置,并根据mysite/settings.py文件中的数据库设置和应用程序附带的数据库迁移创建任何必要的数据库表(稍后我们将介绍这些)
polls/models.py
在该文件定义需要的模型 Question和Choice
from django.db import models
# Create your models here.
class Question(models.Model):
# 该类是django.db.models.Model的子类
question_text = models.CharField(max_length=200)
# question_text 类变量与数据库的字段对应
# CharField 表示是char的数据字段类型
# max_length 表示字符类型存储的最长长度
pub_date = models.DateTimeField('date published')
# DateTimeField是时间字段类型
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
# ForeignKey 定义外键关系
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
# default 表示默认值
配置模型
在创建的Django的项目下的settings.py文件下找到INSTALLED_APPS 并添加 ‘polls.apps.PollsConfig’
INSTALLED_APPS = [
'polls.apps.PollsConfig',# 添加该配置信息
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
并在命令行运行(数据迁移)
py manage.py makemigrations polls
并得到
Migrations for 'polls':
polls\migrations\0001_initial.py
- Create model Choice
- Create model Question
- Add field question to choice
通过该命令会生成一个文件 polls/migrations/0001_initial.py
将模型生成sql
py manage.py sqlmigrate polls 0001
执行该命令应该得到
BEGIN;
--
-- Create model Choice
--
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200)
NOT NULL, "votes" integer NOT NULL);
--
-- Create model Question
--
CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text"
varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE "polls_choice" RENAME TO "polls_choice__old";
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200)
NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id")
DEFERRABLE INITIALLY DEFERRED);
INSERT INTO "polls_choice" ("id", "choice_text", "votes", "question_id") SELECT "id", "choice_text",
"votes", NULL FROM "polls_choice__old";
DROP TABLE "polls_choice__old";
CREATE INDEX "polls_choice_question_id_c5b4b260" ON "polls_choice" ("question_id");
COMMIT;
python manage.py check
# 检查sql命令
py manage.py migrate
# 重新运行
# 将上面变化重新加载到数据库中
注册管理员用户
py manage.py createsuperuser
类似下面
Username: admin
Email address: admin@polls.com
Password:
Password (again):
# 输入密码的时候可能会出现字符未显示的情况,解决:直接输入就行,不管字符显示问题(我用pycharm遇到的情况)
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
启动
py manage.py runserver
在浏览器的地址栏输入http://127.0.0.1:8000/admin/
输入刚才注册的用户名与密码
运行截图
polls/admin.py
在该文件创建polls对象
from django.contrib import admin
from .models import Question
admin.site.register(Question)
刷新刚才的网页出现