SoFunction
Updated on 2024-11-17

python django Add, Delete, and Check Operations Database Mysql

The following is an introduction to the django add, delete, change operation:

1、

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from  import HttpResponse

from  import Test
from  import render

# Create your views here.
# Resolve the garbled code
import sys
reload(sys)
('utf-8')
# Database operations
def testdb(request):
  test1 = Test(name='Wen Hongyu 2')
  ()
  return HttpResponse("<p> Data added successfully! </p>")

# Query the database
def selectDB(request):

  # Get all rows of data through all() of objects, the model manager, which is equivalent to SELECT * FROM in SQL.
  list = ()
  returnvalue = []
  for v in list:
    ()
    print 

  print "++++++++++++ to get a single object ++++++++++++++++++"
  # Get a single object
  response1 = (id=1)
  print response1
  for v1 in response1:
    returnvalue2 = "id : ", , " Name:", 
    print returnvalue2

  print "++++++++++++ limits the data returned Equivalent to OFFSET 0 LIMIT 2 in SQL; ++++++++++++++++++"
  response2 = .order_by('name')[0:2]
  returnvalue3 = {}
  for v2 in response2:
    returnvalue3[] = 

  print ()
  print "+++++++++++ Output: ++++++++++++++++++++++++++++++"
  return HttpResponse(())

# Modify the data using save() or update(): # Save the data using save() or update().
def updateDB(request):
  # Modify one of the name fields with id=1 and then SAVE, equivalent to UPDATE in SQL.
  test1 = (id=1)
   = 'Google'
  ()

  # The other way
  #(id=1).update(name='Google') 
  # Modify all columns
  # ().update(name='Google')

  return HttpResponse("Update data successful.")

def deleteDB(request):
  # Delete data with id=1
  test1 = (id=3)
  ()
  return HttpResponse("Deletion of data successful.")

2、

"""pythondjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
  /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'^$', , 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  import url, include
  2. Add a URL to urlpatterns: url(r'^blog/', include(''))
"""
from  import url
from  import admin
from BlogDjango import views
from polls import views as pollsviews, search, search2

urlpatterns = [
  url(r'^admin/', ),
  url(r'^hello/+\d', ),
  url(r'^base/', ),
  url(r'^testdb$', ),
  url(r'^querydb$', ),
  url(r'^updateDB$', ),
  url(r'^deleteDB$', ),
]

3、

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from  import models

# Create your models here.

class Test():

  name = (max_length=20)

The above this python django add delete change operation database Mysql is all I have to share with you, I hope to be able to give you a reference, but also hope that you support me more.