Python Study Notes _ 04: Django framework introduction, _ 04 django

Source: Internet
Author: User
Tags install django pip install django

Python Study Notes _ 04: Django framework introduction, _ 04 django

Directory

1. What is Django?

2. Build a development environment for the Django framework

3 Introduction to Django operations on MySql Databases

4. Powerful Django management tool applications

 

1 What is Django?

Django is an advanced dynamic language framework applied to Web development. It was originally originated from the Python user group in Chicago, USA, and is a major developer of Django framework. Under the leadership of Adrian, the Django team is committed to contributing an efficient and perfect Python framework to Web developers, and in the BSD (Berkeley Software Distribution, Berkeley Software Package) authorized to developers for free use under the open source code protocol license.

 

Django has a complete template mechanism, object relationship ing mechanism, and functions for dynamically creating the background management interface. By using the Django framework to develop Web applications, you can quickly design and develop Web applications with MVC layers. The Django framework was born from a real project. the functions provided by the Framework are especially suitable for the construction of dynamic websites, especially management interfaces.

 

As a fast network framework, the Django framework has the following features:

  • L reasonable integration of components
  • L object relationship ing and support for most databases
  • L simple URL Design
  • L automated management interface
  • L powerful development environment
2 Build a development environment for the Django framework

Install the Django framework(PS: The method in Windows is described here)

Download link: https://www.djangoproject.com/download/ (PS: web page has detailed installation steps, the reason why the link is posted, easy to view the latest version of the current Django framework and the corresponding version of The Python version supported. This article uses Python 2.7.14)

Open CMD and enter pip install Django = 1.11.7 (PS: 1.11.7 indicates the installed Django version. Pip is a management tool for managing and installing Python-related template racks. If not, install pip in advance. It is very convenient to use pip to install the relevant template rack package)

 

Enable Server

Start a lightweight Web application server included in the Django framework. Therefore, when developing a Web project using the Django framework, you do not need to configure the server for it, for example, Apache configuration. (PS: the lightweight Web application server provided by the Django framework, which is easy to test during development and has low performance. To achieve high performance, redeploy a server after system development, for example, Tomcat, Nginx, and lighttpd in Apache)

Switch to the new project directory in CMD, and then enter the following command in the directory: manage. py runserver (PS: This command enables system Port 8000 by default. If Port 8000 is occupied, you can use manage. py runserver 8001 uses port 8001, or enter another Digital Port)

 

 

3 Introduction to Django operations on MySql Databases

Database Configuration

Find the DATABASES configuration item in the settings. py file of the project and change its information:

DATABASES = {'default': {'Engine ': 'django. db. backends. mysql ', # Or use mysql. connector. django 'name': 'book', # Connection database NAME 'user': 'root', # mysql database connection username 'Password': 'root ', # mysql database connection password 'host': 'localhost', 'Port': '000000', 'options': {'init _ command ': "SET SQL _mode = 'strict _ TRANS_TABLES '", # 'charset': 'utf8mb4 ',}}}

Perform the following steps (for more information, refer to the online tutorial:Django Model):

Create an APP

Django requires that an app must be created to use a model. Run the following command to create an app named TestModel:

django-admin.py startapp TestModel 

Modify the TestModel/models. py file. The Code is as follows:

HelloWorld/TestModel/models. py: File Code:

# models.pyfrom django.db import models class Test(models.Model):    name = models.CharField(max_length=20)

The above class name represents the database table name and inherits the models. model, the fields in the class represent the fields (name) in the data table, the data type is limited by the CharField (equivalent to varchar), DateField (equivalent to datetime), max_length parameter.

Next, find INSTALLED_APPS in settings. py, as follows:

INSTALLED_APPS = ('django. contrib. admin', 'django. contrib. auth ', 'django. contrib. contenttypes ', 'django. contrib. sessions ', 'django. contrib. messages ', 'django. contrib. staticfiles ', 'testmodel', # add this item)

Run:

$ Python manage. py migrate # create a table structure $ python manage. py makemigrations TestModel # Let Django know that we have some changes in our model $ python manage. py migrate TestModel # create a table structure

See several rows of "Creating table... ", Your data table is created.

Creating tables ......... Creating table TestModel_test # custom table ......

The table name structure is: Application name_class name (for example, TestModel_test ).

Note: although we didn't set a primary key for the table in models, Django automatically adds an id as the primary key.

The tutorial includes adding, updating, and deleting data. For more information, see the link to the above tutorial.

 

 

 

4 Powerful Django management tool Application

First, check the running effect of the management tool:

 

Figure 1 homepage login page, beautiful

 

Figure 2 after logging on to the management interface, the Django framework comes with a line of code.

 

Figure 3 operation background contact entity table, which can be added, deleted, modified, and queried

 

For specific code, see: https://coding.net/u/LiuZhen1995/p/MyDemo/git/tree/origin_nine/

 

 

References:

1. Edited by LI Yong Wang Wenqiang, Python Web development learning transcript

2. http://www.runoob.com/django/django-tutorial.html

3. https://code.ziqiangxuetang.com/django/django-tutorial.html

 

 

 

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.