Python django adds, deletes, modifies, and queries the database Mysql, pythondjango

Source: Internet
Author: User

Python django adds, deletes, modifies, and queries the database Mysql, pythondjango

The following describes how to add, delete, modify, and query django:

1. view. py

#-*-Coding: UTF-8-*-from _ future _ import unicode_literalsfrom django. http import HttpResponsefrom polls. models import Testfrom django. shortcuts import render # Create your views here. # solve the garbled import sysreload (sys) sys. setdefaultencoding ('utf-8') # Database Operation def testdb (request): test1 = Test (name = 'wen Hongyu 2') test1.save () return HttpResponse ("<p> data added successfully! </P> ") # query database def selectDB (request): # obtain all data rows through all () of the objects model manager, which is equivalent to SELECT * FROM list = Test in SQL. objects. all () returnvalue = [] for v in list: returnvalue. append (v. name) print v. name print "++ get a single object ++" # Get a single object object response1 = Test. objects. filter (id = 1) print response1 for v1 in response1: returnvalue2 = "id:", v1.id, "Name :", v1.name print returnvalue2 print "++ restricts that the returned data is equivalent to OFFSET 0 LIMIT 2 in SQL; ++ "response2 = Test. objects. order_by ('name') [0: 2] returnvalue3 ={} for v2 in response2: returnvalue3 [v2.id] = v2.name print returnvalue3.items () print "++ output result: ++ "return HttpResponse (returnvalue3.items ()) # To modify data, you can use save () or update (): def updateDB (request): # modify the name field of one of the IDS = 1, and then save, it is equivalent to UPDATE test1 = Test. objects. get (id = 1) test1.name = 'Google 'test1.save () # Another method # Test. objects. filter (id = 1 ). update (name = 'Google ') # modify all columns # Test. objects. all (). update (name = 'Google ') return HttpResponse ("data updated successfully") def deleteDB (request): # delete data test1 = Test with id = 1. objects. get (id = 3) test1.delete () return HttpResponse ("data deleted successfully ")

2. urls. py

"""pythondjango URL ConfigurationThe `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 urlfrom django.contrib import adminfrom BlogDjango import viewsfrom polls import views as pollsviews, search, search2urlpatterns = [  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_literalsfrom django.db import models# Create your models here.class Test(models.Model):  name = models.CharField(max_length=20)

The above python django addition, deletion, modification, query, and Operation database Mysql is all the content that I have shared with you. I hope to provide you with a reference and support for more customers.

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.