Django and its configuration (Mysql)

Source: Internet
Author: User

MVC and MTV Mode

The famous MVC pattern: the so-called MVC is to divide Web applications into models (M), controllers (C), Views (V), and they are connected together in a plug-in, loosely coupled way.

The model is responsible for the business object and the database object (ORM), the view is responsible for interacting with the user (page), the controller (C) receives the user's input call model and the view completes the user's request.

The Django MTV model is essentially no different from the MVC pattern, and it's just a bit different from the definition of the components to keep the relationship loose, and Django's MTV represents:

  mode (model): object that is responsible for business objects and databases (ORM)

  template: How to show the page to the user

  View: Responsible for business logic, and call model and template when appropriate

In addition, Django also has a URL dispatcher, which is to send a URL page request to different view processing, the view then call the corresponding model and Templat

Django Configuration

Django#Installation: PIP3 install DjangoAdding environment Variables#1 Creating Projectdjango-Admin Startproject MySite---MySite---settings.py---url.py---wsgi.py----manage.py (startup file)#2 Creating an apppython mannage.py startapp app01#3 Settings ConfigurationTEMPLATES Staticfiles_dirs=(Os.path.join (Base_dir,"statics"),) Static_url='/static/'        #we can only use Static_url, but Static_url will follow your staticfiles_dirs to find    #4 design code based on requirementsurl.py view.py#5 using TemplatesRender (req,"index.html")       #6 starting the projectPython manage.py runserver 127.0.0.1:8090#7 Connect to database, manipulate datamodel.py
Django Implementation Process

Diango command-line tools

django-admin.py is a Django command-line tool for managing tasks, manage.py is a simple wrapper for django-admin.py, and each Django project will have a mannage.py.

<1> Create a Django project: django-admin.py startproject MySite

The current directory will generate a MySite project with the following directory structure:

    • manage.py-----A tool inside a Django project that can invoke Django shells and databases.
    • The settings.py----contains the default settings for the project, including database information, debug flags, and other work variables.
    • The urls.py-----is responsible for mapping the URL pattern to the application.

<2> Create a blog app in the MySite directory: python manage.py startapp blog

<3> start a Django project:python manage.py runserver 8080

So our Django is up and up! When we visit: http://127.0.0.1:8080/, we can see:

<4> Generate scripts for synchronizing databases:python manage.py makemigrations

Synchronizing databases: python manage.py migrate

Note: In the development process, after the database synchronization error operation, it is inevitable that after the failure to synchronize the success of the situation, a simple and brutal way to solve this problem is to put the migrations directory

Scripts (except __init__.py) are all erased, and then the database is deleted and a new database is created, and the database synchronization operation is re-done again.

For example: If we are using MySQL, we need to change the configuration file first, then change the __init__, modify the model.py, and finally pre-create the database in MySQL we need to use.

<5> emptying the database:python manage.py flush

Django and its configuration (Mysql)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.