Django introduction installation and creation

Time:2021-10-24

Django installation

Command line installation

pip install django==1.11.9

Pycharm installation

Django安装

Django basic operation

Command line operation

#1. Create Django project
#Website is a user-defined item name, which can be changed
django-admin startproject website

#2. Launch Django project
#Switch the path to the project path before starting the project
cd D:/website
python manage.py runserver

#3. Create an application
#Application01 is a custom application name, which can be changed
python manage.py startapp application01

Django欢迎界面

Pycharm operation

  1. Create Django project: new project select Django to create Django project (only supported by pycharm Professional Edition)

创建Django项目

  1. Launch Django project
    • Method 1: start from the command line
    • Method 2: click the green arrow in the upper right corner of pycharm to start

启动Django项目

  1. Create application
    • Method 1: directly create a project through pycharm. Only one project can be created
    • Method 2: create from the command line
    • Method 3: create a task through manage.py

manage.py Task

Introduction to main documents of Django

  • Website site project folder
    • Web site folder with the same name
    • Manage.py Django entry file
    • Db.sqlite3 Django’s own database
    • Application01 application folder
      • Admin.py Django background management
      • Apps.py registered for use
      • Migrations folder database migration records
      • Models.py database related model class (ORM)
      • Tests.py test file
      • Views.py view layer

Difference between command line and pycharm

  1. When pycharm creates a project, it automatically creates a Templates folder, which needs to be created manually on the command line
  2. When pycharm creates an application, it will automatically configure the application path in the setting.py configuration file, and the command line needs to be added manually
#To create an application, you must configure the application path in the configuration file

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #Configuration mode 1: complete form
    'application01.apps.Application01Config',
    
    #Configuration mode 2: short form
    # 'application1',
]

Introduction to setting configuration file

  • BASE_DIRCurrent project path
  • DEBUGCode debugging. After the project goes online, the result needs to be changed toFalse
  • ALLOWED_HOSTSHosts allowed to access
  • INSTALLED_APPSFor registered applications, Django comes with six function modules by default
  • MIDDLEWAREDjango Middleware
  • TEMPLATESHTML file storage path configuration
  • DATABASESDatabase specified by the project

Current environment:
Python version: Python 3.6.8
Django version: Django 1.11.9