最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python django 增刪改查操作 數(shù)據(jù)庫Mysql

 更新時(shí)間:2017年07月27日 08:51:17   投稿:jingxian  
下面小編就為大家?guī)硪黄猵ython django 增刪改查操作 數(shù)據(jù)庫Mysql。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

下面介紹一下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')
# 數(shù)據(jù)庫操作
def testdb(request):
  test1 = Test(name='溫鴻雨2')
  test1.save()
  return HttpResponse("<p>數(shù)據(jù)添加成功!</p>")

# 查詢數(shù)據(jù)庫
def selectDB(request):

  # 通過objects這個(gè)模型管理器的all()獲得所有數(shù)據(jù)行,相當(dāng)于SQL中的SELECT * FROM
  list = Test.objects.all()
  returnvalue = []
  for v in list:
    returnvalue.append(v.name)
    print v.name

  print "++++++++++++獲取單個(gè)對(duì)象++++++++++++++++++"
  # 獲取單個(gè)對(duì)象
  response1 = Test.objects.filter(id=1)
  print response1
  for v1 in response1:
    returnvalue2 = "id : ", v1.id, " 姓名:", v1.name
    print returnvalue2

  print "++++++++++++限制返回的數(shù)據(jù) 相當(dāng)于 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 "+++++++++++輸出結(jié)果:++++++++++++++++++++++++++++++"
  return HttpResponse(returnvalue3.items())

#修改數(shù)據(jù)可以使用 save() 或 update():
def updateDB(request):
  # 修改其中一個(gè)id=1的name字段,再save,相當(dāng)于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("更新數(shù)據(jù)成功")

def deleteDB(request):
  # 刪除id=1的數(shù)據(jù)
  test1 = Test.objects.get(id=3)
  test1.delete()
  return HttpResponse("刪除數(shù)據(jù)成功")

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 增刪改查操作 數(shù)據(jù)庫Mysql就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python xml解析實(shí)例詳解

    python xml解析實(shí)例詳解

    這篇文章主要介紹了python xml解析實(shí)例詳解的相關(guān)資料,這里舉例說明如何實(shí)現(xiàn),需要的朋友可以參考下
    2016-11-11
  • django中cookiecutter的使用教程

    django中cookiecutter的使用教程

    這篇文章主要給大家介紹了關(guān)于django中cookiecutter使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 詳細(xì)解讀Python中的json操作

    詳細(xì)解讀Python中的json操作

    json操作是最為基本的、最為常用的,Python自帶的json模塊就可以滿足大部分應(yīng)用場(chǎng)景,而且使用起來極為簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于Python中json操作的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • python制作簡(jiǎn)單五子棋游戲

    python制作簡(jiǎn)單五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了python制作簡(jiǎn)單五子棋游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • python?reshape和transpose的區(qū)別小結(jié)

    python?reshape和transpose的區(qū)別小結(jié)

    reshape()?和?transpose()?是用于改變數(shù)組或張量形狀的兩種不同方法,本文主要介紹了python?reshape和transpose的區(qū)別小結(jié),具有一定參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Python圖像灰度變換及圖像數(shù)組操作

    Python圖像灰度變換及圖像數(shù)組操作

    這篇文章主要介紹了Python圖像灰度變換及圖像數(shù)組操作的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Python極簡(jiǎn)代碼實(shí)現(xiàn)楊輝三角示例代碼

    Python極簡(jiǎn)代碼實(shí)現(xiàn)楊輝三角示例代碼

    楊輝三角形因?yàn)槠湫问胶?jiǎn)單,又有一定的使用價(jià)值,因此是入門編程題中被用的最多的,也是很好的語言實(shí)例標(biāo)的。這篇文章就給大家介紹了Python極簡(jiǎn)代碼實(shí)現(xiàn)楊輝三角的方法,文章給出了詳細(xì)的示例代碼和解釋,對(duì)大家理解很有幫助,感興趣的朋友們下面來一起看看吧。
    2016-11-11
  • python django 訪問靜態(tài)文件出現(xiàn)404或500錯(cuò)誤

    python django 訪問靜態(tài)文件出現(xiàn)404或500錯(cuò)誤

    這篇文章主要介紹了python django 訪問靜態(tài)文件出現(xiàn)404或500錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 在Python中編寫數(shù)據(jù)庫模塊的教程

    在Python中編寫數(shù)據(jù)庫模塊的教程

    這篇文章主要介紹了在Python中編寫數(shù)據(jù)庫模塊的教程,本文代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python folium的實(shí)用功能詳解

    Python folium的實(shí)用功能詳解

    這篇文章主要為大家詳細(xì)介紹了Python中folium的使用功能,圖層控制、指北針、folium添加js和css、經(jīng)緯網(wǎng)格線(柵格線)等相關(guān)內(nèi)容,感興趣的小伙伴可以了解一下
    2022-12-12

最新評(píng)論

沁水县| 乐山市| 祁东县| 太康县| 休宁县| 邢台县| 原阳县| 东莞市| 天镇县| 保靖县| 临邑县| 仙桃市| 巫山县| 双江| 道真| 文登市| 黎城县| 福州市| 蕉岭县| 怀集县| 黔南| 长宁县| 皋兰县| 东明县| 宁蒗| 商水县| 河西区| 班戈县| 栾川县| 邮箱| 拜城县| 达尔| 安国市| 兴义市| 襄汾县| 西林县| 茂名市| 镇原县| 镇沅| 山东省| 田东县|