Django


Linux 上安装 Django

  • https://www.djangoproject.com/download/

pip3 install Django

验证

python3
import django
django.VERSION

Django 管理工具

django-admin

✦ ❯ django-admin

Type 'django-admin help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    runserver
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

创建项目

创建完成后可以查看下项目的目录结构

启动服务器

常见问题

  • Django 遇到 Invalid HTTP_HOST header

    • 修改 settings.py ALLOWED_HOSTS 值为 '*'

视图和 URL 配置

testweb 目录新建一个 views.py 文件

修改 urls.py

path() 函数

Django path() 可以接收四个参数,分别是两个必选参数:route、view 和两个可选参数:kwargs、name。

语法格式:


debug

settings.py


Source & Reference

  • https://www.runoob.com/django/django-template.html

  • https://www.runoob.com/django/django-model.html

  • https://www.runoob.com/django/django-views.html