当前位置:   article > 正文

【django基础】django相关常用命令_django命令

django命令

django相关常用命令

1. 创建项目

django-admin startproject mysite
  • 1

执行完命令后,会生成以下文件

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. 服务启动

python manage.py runserver
# 可以指定ip和端口号
python manage.py runserver 8080
python manage.py runserver 0:8000
  • 1
  • 2
  • 3
  • 4

3. 创建应用

python manage.py startapp polls
  • 1

这将会创建一个 polls 目录,它的目录结构大致如下:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4. 创建表(模型)

4.1 创建相关表

这个migrate命令检查INSTALLED_APPS 设置,为其中的每个应用创建需要的数据表。 只会迁移INSTALLED_APPS中的

python manage.py migrate
  • 1

4.2 迁移表模型

迁移表模型,需要指定app名称

python manage.py makemigrations polls
  • 1

通过运行 makemigrations 命令,Django 会检测你对模型文件的修改(在这种情况下,你已经取得了新的),并且把修改的部分储存为一次 迁移

注意

  1. makemigrations会在当前目录下生成一个migrations文件夹,该文件夹的内容就是数据库要执行的内容
    python manage.py makemigrations
  2. migrate就是执行之前生成的migrations文件,这一步才是操作数据库的一步
    python manage.py migrate
    Django每次更新模型都需要执行以上两步,需要注意的是Django模型增加内容需要设定变量的初始值,否则会在第一步出现问题

4.3 生成表的sql

python manage.py sqlmigrate polls 0001
  • 1

返回对应的sql

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;
  • 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

5. 交互式 Python 命令行

python manage.py shell
  • 1

这个就可以对模型(表)进行访问了

>>> from polls.models import Choice, Question  # 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.
>>> 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 (1)>]>
  • 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

6. Django管理页面

6.1 创建管理员账号

python manage.py createsuperuser
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/865137
推荐阅读
相关标签
  

闽ICP备14008679号