Django - Patterns, Simple to Use

The famous MVC pattern: The so-called MVC is to divide the web application into three layers: model (M), controller (C), and view (V); they are connected together in a plug-in-like, loosely coupled manner.

  • Models are responsible for business objects and database objects (ORM)
  • The view is responsible for the interaction with the user (the page)
  • The controller (C) accepts the user's input and invokes the model and view to complete the user's request.

  

 

Django is an MTV model, there is no essential difference between the two, and it is also to maintain a loosely coupled relationship between components .

  • Model: Object (ORM) responsible for business objects and databases
  • Template (template): responsible for how to display the page to the user----------V
  • View (view): responsible for business logic, and call Model and Template when appropriate---------------C

Django also has a url dispatcher, which is used to distribute page requests of one URL to different views for processing, and then the view calls the corresponding Model and Template.

 

 django process and command line                                    

 1. Create a Django object

django-admin startproject mysite

  

  • manage.py ----- Tools in the Django project, through which you can call the django shell and database.
  • settings.py ---- Contains the default settings for the project, including database information, debug flags, and other variables that work.
  • urls.py ----- Responsible for mapping URL patterns to applications.

2. Create an application

python manage.py startapp blog (app name)     

  This is what a blank pro contains, one more file for each additional application

C:.
│  db.sqlite3
│  manage.py
├─blog
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  views.py
│  │  __init__.py
│  │
│  └─migrations__init__.py
└─mysite
    │  settings.py
    │  urls.py
    │  wsgi.py__init__.py
    └─__pycache__
            settings.cpython-35.pyc
            urls.cpython-35.pyc
            wsgi.cpython-35.pyc
            __init__.cpython-35.pyc

Add blog to INSTALLED_APPS in settings.py

 

3, settings configuration

TEMPLATES

       STATICFILES_DIRS=(
            os.path.join(BASE_DIR,"statics"),
        )

       STATIC_URL = ' /static/ '  
       #We   can only use STATIC_URL, but STATIC_URL will be found according to your STATICFILES_DIRS #4 Design the code according to your needs 
           url.py
           view.py

4. Use templates

render(req,"index.html")  

5. Start the Django project

python  manage.py runserver  127.0.0.1:8090
6. Connect to the database and manipulate data
Create table: python manage.py makemigrations  

Sync database: python manage.py migrate  

  Note: During the development process, after the database synchronization misoperation, it is inevitable to encounter the situation that the synchronization cannot be successful later. A simple and rude way to solve this problem is to delete all the scripts in the migrations directory (except __init__.py) Delete the database, create a new database after deleting the database, and do the database synchronization operation again.     

7. When accessing http://127.0.0.1:8000/admin/:  

So we need to create a super administrator for entering the background of this project: the database needs to be synchronized first

python manage.py createsuperuser

  

  After you set your username and password, you can log in!

  

8. Clear the database

python manage.py  flush

9. Query the detailed information of a command

jango-admin.py  help  startapp

10. Start the interactive interface

python manage.py  shell

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325288995&siteId=291194637