


Detailed explanation of django's addition, deletion, modification and query operations on database Mysql in python
The following editor will bring you an article about python django add, delete, modify and query database Mysql. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
The following introduces the django add, delete, modify and check operations:
1. view .py
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.http import HttpResponse from polls.models import Test from django.shortcuts import render # Create your views here. # 解决乱码 import sys reload(sys) sys.setdefaultencoding('utf-8') # 数据库操作 def testdb(request): test1 = Test(name='温鸿雨2') test1.save() return HttpResponse("<p>数据添加成功!</p>") # 查询数据库 def selectDB(request): # 通过objects这个模型管理器的all()获得所有数据行,相当于SQL中的SELECT * FROM list = Test.objects.all() returnvalue = [] for v in list: returnvalue.append(v.name) print v.name print "++++++++++++获取单个对象++++++++++++++++++" # 获取单个对象 response1 = Test.objects.filter(id=1) print response1 for v1 in response1: returnvalue2 = "id : ", v1.id, " 姓名:", v1.name print returnvalue2 print "++++++++++++限制返回的数据 相当于 SQL 中的 OFFSET 0 LIMIT 2;++++++++++++++++++" response2 = Test.objects.order_by('name')[0:2] returnvalue3 = {} for v2 in response2: returnvalue3[v2.id] = v2.name print returnvalue3.items() print "+++++++++++输出结果:++++++++++++++++++++++++++++++" return HttpResponse(returnvalue3.items()) #修改数据可以使用 save() 或 update(): def updateDB(request): # 修改其中一个id=1的name字段,再save,相当于SQL中的UPDATE test1 = Test.objects.get(id=1) test1.name = 'Google' test1.save() # 另外一种方式 #Test.objects.filter(id=1).update(name='Google') # 修改所有的列 # Test.objects.all().update(name='Google') return HttpResponse("更新数据成功") def deleteDB(request): # 删除id=1的数据 test1 = Test.objects.get(id=3) test1.delete() return HttpResponse("删除数据成功")
2、urls.py
"""pythondjango URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url from django.contrib import admin from BlogDjango import views from polls import views as pollsviews, search, search2 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^hello/+\d', views.hello), url(r'^base/', views.base), url(r'^testdb$', pollsviews.testdb), url(r'^querydb$', pollsviews.selectDB), url(r'^updateDB$', pollsviews.updateDB), url(r'^deleteDB$', pollsviews.deleteDB), ]
3、models.py
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models # Create your models here. class Test(models.Model): name = models.CharField(max_length=20)
The above is the detailed content of Detailed explanation of django's addition, deletion, modification and query operations on database Mysql in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

Discussion on Hierarchical Structure in Python Projects In the process of learning Python, many beginners will come into contact with some open source projects, especially projects using the Django framework...
