Home >Backend Development >Python Tutorial >How to use Django to connect to the database in python (picture and text)

How to use Django to connect to the database in python (picture and text)

不言
不言forward
2018-09-28 14:40:313846browse

The content of this article is about how to use Django to connect to the database in python (pictures and texts). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Three elements of form form submission data:

1. The form form tag must have action and method attributes; the value of the action key represents the submission address of the information in the html page. The value of the method key indicates the method used to submit

2. All tags that obtain user input must be placed in the form and must have a name attribute; the value of the name key indicates the information to be submitted

3. There must be a submit button.

request related attributes:

request refers to the request sent by the browser to the server.

1. request.method -- What is returned is the requested method (all uppercase): GET POST...

Note: Watch the method source code to learn:

## 

In HTML pages, there are often "method='post'" and the like that stipulate that the method should be lowercase when obtaining the value. In this case, both upper and lower case are acceptable. When the value is obtained from the method, ' upper' will capitalize all the request methods in the method.

2. request.GET - Obtain the parameters in the URL, a data structure similar to a dictionary (if submitted using the get method, use GET to obtain)

3. request.POST -- The data submitted by post is similar to a dictionary data structure (if you submit it using the post method, you must use POST to obtain it)

Django's template language {{variable name }}

Connect to mysql

Use ORM (Object Relationship Model) to translate SQL statements.

Advantages: High development efficiency; No need to write SQL statements directly during development

Disadvantages: low execution efficiency

Create app application in Django project

Create a Python package in the project, different functions are placed in different packages,

Create app --Python manage.py startapp app name (example: app01)

Indicates in Django that an app has been created:

Find INSTALLED_APPS in settings.py and add the new app



Usage of ORM in Django

Use: Operation data table operation data row

Usage:

1. Manually create a database (ORM cannot create a database) --- create databasemysite;

2. Display in Django The database to be connected


#3. Indicate the way to connect to the database (using a third-party package)

In Django It shows that the pymysql module is used instead of the default MySQLdb to connect to the MySQL database:

Configure in the _init_.py file at the same level as settings.py:


 

4. Create a class (customized class name) in the models.py file under the app application. This class must inherit models.Model, You can use ORM language to write the table structure in the created class

5. Execute the command and sentence to complete the operation of the table in the database

python manage. py makemigrations -- Record the changes in models.py and update the operations to be performed on the database

python manage.py migrate -- Translate the change records into SQL statements and complete the operations in the database.


 

Connect to the database

Click database to create a database and connect to the database:

## ORM query

User.objects.filter(email='', pwd='')

The above is the detailed content of How to use Django to connect to the database in python (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete