django 项目初始化
1 2 | django-admin.py startproject HelloWorld python manage.py runserver 0.0.0.0:8000 |
1 2 3 4 5 | django-admin startproject mysite python manage.py runserver python manage.py runserver 8080 python manage.py runserver 0:8000 |
创建应用:
1 | python manage.py startapp polls |
1 | 127.0.0.1:8000 /admin |
进到管理员登录页
在你当前目录下创建一个 mysite 目录
1 | django-admin startproject mysite |
新生成的目录结构:
1 2 3 4 5 6 7 | mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py |
创建数据库:
1 | python manage.py migrate |
运行服务:
1 | python manage.py runserver |
或:
1 2 | python manage.py runserver 8080 python manage.py runserver 0.0.0.0:8000 |
详见:
输出:
1 2 3 4 5 6 7 | Performing system checks... 0 errors found February 23, 2017 - 15:50:53 Django version 1.8, using settings 'mysite.settings' Starting development server at http: //127 .0.0.1:8000/ Quit the server with CONTROL-C. |
新建应用:
1 | python manage.py startapp polls |
建出目录:
1 2 3 4 5 6 7 8 | polls / __init__.py admin.py migrations / __init__.py models.py tests.py views.py |
当模型(数据字段和结构等)有改动后:
1 | python manage.py makemigrations polls |
输出:
1 2 3 4 5 | Migrations for 'polls' : 0001_initial.py: - Create model Question - Create model Choice - Add field question to choice |
将迁移自动应用于数据库表
1 | python manage.py sqlmigrate polls 0001 |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | BEGIN; CREATE TABLE "polls_choice" ( "id" serial NOT NULL PRIMARY KEY, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL); CREATE TABLE "polls_question" ( "id" serial NOT NULL PRIMARY KEY, "question_text" varchar(200) NOT NULL, "pub_date" timestamp withtime zone NOT NULL); 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; |
再去真正去迁移数据库==将尚未应用的迁移数据应用到数据库中:
1 | python manage.py migrate |
输出:
1 2 3 4 5 6 7 8 9 10 | Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, polls, auth, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying <migration name>... OK |
当修改数据库相关字段和结构后:
- 修改模型 (in models.py).
- 运行 python manage.py makemigrations 创建对模型变化的迁移数据
- 运行 python manage.py migrate 将迁移应用到数据库中
引申:
1 | python manage.py shell |
可以去了解和学习Django的shell的api
如果你不想使用 manage.py,那么也可以代码中调用:
在和 manage.py 文件同级目录下
1 2 | >>> import django >>> django.setup() |
在shell命令行下,就可以开始探索 数据库 API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # 导出我们之前创建的模型类 >>> from polls.models import Question, Choice # 目前系统还没有任何数据 >>> Question.objects. all () [] # 创建一个新的 Question。 # 默认配置下"时区"是开启的 # Django是通过tzinfo来预测日期和时间的。使用timezone.now() # 与datetime.datetime.now()的值相同 >>> from django.utils import timezone >>> q = Question(question_text = "What's new?" , pub_date = timezone.now()) # 要保存该对象到数据库,须要明确调用函数 save() >>> q.save() # 现在该对象就有了一个 ID 号。注意该值可能显示为 "1L" 或者是 "1",这取决于你所使用的数据库,这并不重要,只是数据库后端中返回的整形值,对应为Python的长整型值。 >>> q. id 1 # 通过Python属性的方式来访问数据模型值。 >>> q.question_text "What's new?" >>> q.pub_date datetime.datetime( 2012 , 2 , 26 , 13 , 0 , 0 , 775217 , tzinfo = <UTC>) # 修改了变量的值后,需要调用函数 save() >>> q.question_text = "What's up?" >>> q.save() # objects.all() 返回数据库所有 questions记录 >>> Question.objects. all () [<Question: Question object >] |
后续:
创建一个具有登录到管理站点权限的账户
1 | python manage.py createsuperuser |
输入
账户名
邮箱
密码
在:
1 | python manage.py runserver |
后,去访问
看到登录界面
官网教程:
中文版:
写好model后,去创建数据库:
1 | python manage.py migrate |
其他更多操作方式:
- migrate
- which is responsible for applying and unapplying migrations.
- makemigrations
- which is responsible for creating new migrations based on the changes you have made to your models.
- sqlmigrate
- which displays the SQL statements for a migration.
- showmigrations
- which lists a project’s migrations and their status.
1 2 3 4 5 6 7 | $ pip3 install virtualenv $ virtualenv --python=` which python3` ~/.virtualenvs /djangodev $ source ~/.virtualenvs /djangodev/bin/activate $ pip install -r requirements /py3 .txt $ pip install Django |
转载请注明:在路上 » 【整理】Django项目初始化流程