Django framework

Time:2021-9-6

Django framework

1、 Django frame installation

Version problem

1. X version

2. X version

Recommended version 1. X:

      1.11.09~1.11.13

pip3 install django==1.11.11

 

 

2、 Two ways to create Django projects

1. Command line creation

1) Create Django project

Django admin startproject MySite # MySite is the project name

File directory of the project:

mysite:
    - mysite
        - __init__.py
        - asgi.py
        - settings.py
        - urls.py
        - wsgi.py
    - manage.py

2) Create an app with independent functions

Python manage.py startapp app01 # app01 is a custom app name

File directory of the project:

app01:
    - migrations
        - __init__.py
    - __init__.py
    - admin.py
    - apps.py
    - models.py
    - tests.py
    - views.py

3) Start Django project (switch to the project directory first)

Python 3 manage.py runserver # Django default port number is 8000

 

2. Pycharm quick create

 

3. The difference between the command line and pycharm in creating Django projects

1) The Django project created using the command line will not automatically create the templates template folder, but can only be created manually

2) Common Django projects on the command line not only do not have Templates folder configuration files, but also do not fill in paths. Projects created by pycharm will be added automatically

 

 

3、 Concept of app

Django is actually a web framework that focuses on developing apps

An empty Django project is similar to a university, and each app is similar to each college in the University. Each app is actually similar to different functional modules

Example: shopping website

User related user

User related apps

Order related

Order related apps

Complaints related to complaints

Complaints related apps

It is recommended to use different apps to develop different functional blocks

Django supports multiple apps       app01, app02…

 

 

4、 Main file functions of Django

mysite:
    - mysite
        - __init__.py
        - asgi.py
        -Settings.py # project configuration file
        -Urls.py # route view function correspondence, total route of the project
        - wsgi.py
    - manage.py
    - app01
        -Migrations # database change record
            - __init__.py
        - __init__.py
        -Admin.py # Django background management
        -Apps.py # register app related
        -Models.py # model class (ORM)
        -Tests.py # test file
        -Views.py # view function (top priority, logic code)
    - app02
        -Migrations # database change record文件夹,当改动数据库时,会产生相应的py文件
            - __init__.py
        - __init__.py
        -Admin.py # Django background management
        -Apps.py # register app related
        -Models.py # model class (ORM)
        -Tests.py # test file
        -Views.py # view function (top priority, logic code)
    -Db.sqlite3 # Django comes with a small database for local testing (not very sensitive to date format data)
    ...