python django 增删改查操作 数据库Mysql

yipeiwu_com5年前Python基础

下面介绍一下django增删改查操作:

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)

以上这篇python django 增删改查操作 数据库Mysql就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python从函数参数类型引出元组实例分析

本文实例讲述了Python从函数参数类型引出元组。分享给大家供大家参考,具体如下: 自定义函数:特殊参数 def show(name="jack", *info): print(...

python3 打开外部程序及关闭的示例

如下所示: import os import time import subprocess subprocess.Popen(r'cmd') print('打开成功') time...

python实现探测socket和web服务示例

操作系统:linux软件环境:Python 2.7.3 用法:复制代码 代码如下:$ ./MonSocket.py # This is check the URI or Socket...

Python多线程处理实例详解【单进程/多进程】

Python多线程处理实例详解【单进程/多进程】

本文实例讲述了Python多线程处理操作。分享给大家供大家参考,具体如下: python — 多线程处理 1、一个进程执行完后,继续下一个进程 root@72132server:~#...

Python适配器模式代码实现解析

Python适配器模式,代码,思考等 # -*- coding: utf-8 -*- # author:baoshan class Computer: def __init__(...