search
HomeBackend DevelopmentPython TutorialHow to use Django to connect to the database in python (picture and text)

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:博客园. If there is any infringement, please contact admin@php.cn delete
What are the alternatives to concatenate two lists in Python?What are the alternatives to concatenate two lists in Python?May 09, 2025 am 12:16 AM

There are many methods to connect two lists in Python: 1. Use operators, which are simple but inefficient in large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use the = operator, which is both efficient and readable; 4. Use itertools.chain function, which is memory efficient but requires additional import; 5. Use list parsing, which is elegant but may be too complex. The selection method should be based on the code context and requirements.

Python: Efficient Ways to Merge Two ListsPython: Efficient Ways to Merge Two ListsMay 09, 2025 am 12:15 AM

There are many ways to merge Python lists: 1. Use operators, which are simple but not memory efficient for large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use itertools.chain, which is suitable for large data sets; 4. Use * operator, merge small to medium-sized lists in one line of code; 5. Use numpy.concatenate, which is suitable for large data sets and scenarios with high performance requirements; 6. Use append method, which is suitable for small lists but is inefficient. When selecting a method, you need to consider the list size and application scenarios.

Compiled vs Interpreted Languages: pros and consCompiled vs Interpreted Languages: pros and consMay 09, 2025 am 12:06 AM

Compiledlanguagesofferspeedandsecurity,whileinterpretedlanguagesprovideeaseofuseandportability.1)CompiledlanguageslikeC arefasterandsecurebuthavelongerdevelopmentcyclesandplatformdependency.2)InterpretedlanguageslikePythonareeasiertouseandmoreportab

Python: For and While Loops, the most complete guidePython: For and While Loops, the most complete guideMay 09, 2025 am 12:05 AM

In Python, a for loop is used to traverse iterable objects, and a while loop is used to perform operations repeatedly when the condition is satisfied. 1) For loop example: traverse the list and print the elements. 2) While loop example: guess the number game until you guess it right. Mastering cycle principles and optimization techniques can improve code efficiency and reliability.

Python concatenate lists into a stringPython concatenate lists into a stringMay 09, 2025 am 12:02 AM

To concatenate a list into a string, using the join() method in Python is the best choice. 1) Use the join() method to concatenate the list elements into a string, such as ''.join(my_list). 2) For a list containing numbers, convert map(str, numbers) into a string before concatenating. 3) You can use generator expressions for complex formatting, such as ','.join(f'({fruit})'forfruitinfruits). 4) When processing mixed data types, use map(str, mixed_list) to ensure that all elements can be converted into strings. 5) For large lists, use ''.join(large_li

Python's Hybrid Approach: Compilation and Interpretation CombinedPython's Hybrid Approach: Compilation and Interpretation CombinedMay 08, 2025 am 12:16 AM

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

Learn the Differences Between Python's 'for' and 'while' LoopsLearn the Differences Between Python's 'for' and 'while' LoopsMay 08, 2025 am 12:11 AM

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

Python concatenate lists with duplicatesPython concatenate lists with duplicatesMay 08, 2025 am 12:09 AM

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version