The following small series for everyone to bring a Python django additions and deletions to change the operation database MySQL. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Here's a look at Django additions and deletions:
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.# address garbled import sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') # database Operation def testdb (request): Test1 = Test (name= ' Win Hong Rain 2 ') Test1.save () return HttpResponse ("<p> data added successfully! </p> ") # Query Database def selectdb (request): # Get all data rows by objects all () of this model manager, equivalent to the SELECT * from list in sql = Test.objects.all () returnvalue = [] for V in List:returnvalue.append (v.name) print v.name print "++++++++++++ get a single object +++++++++++++ +++++ "# Gets a single object response1 = Test.objects.filter (id=1) print response1 for v1 in response1:returnvalue2 =" ID: ", V1.id, "Name:", v1.name print returnvalue2 print "++++++++++++ limit returned data 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: ++++++++++++++++++++++++++++++ "return HttpResponse (Returnvalue3.items ()) #修改数据可以使用 Save () or update (): def updatedb (Request): # Modify one of the Id=1 's name fields, then save, equivalent to update test1 in sql = Test.objects.get (id=1) test1.name = ' Google ' t Est1.save () # Another way to #Test. Objects.filter (id=1). Update (name= ' Google ') # Modify all Columns # Test.objects.all (). Update (name= ' Go Ogle ') return HttpResponse ("Update Data Success") def Deletedb (request): # delete id=1 Data test1 = Test.objects.get (id=3) Test1.delete () Return HttpResponse ("delete data succeeded")
2, urls.py
"" "Pythondjango URL configurationthe ' urlpatterns ' list routes URLs to views. For more information 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 impo RT 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), u RL (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)