当前位置: 首页 > news >正文

西安网站建设官网揭阳百度快照优化排名

西安网站建设官网,揭阳百度快照优化排名,网络营销推广方法和工具有哪些,网站建设 质量标准文章目录 连接Mysql数据库安装Mysql驱动配置数据库信息明确连接驱动定义模型在模型下的models.py中定义表对象在settings.py 中找到INSTALLED_APPS添加创建的模型 测试testdb.py中写增删改查操作urls.py添加请求路径启动项目进行测试 连接Mysql数据库 安装Mysql驱动 pip inst…

文章目录

  • 连接Mysql数据库
    • 安装Mysql驱动
    • 配置数据库信息
    • 明确连接驱动
    • 定义模型
      • 在模型下的models.py中定义表对象
      • 在settings.py 中找到INSTALLED_APPS添加创建的模型
  • 测试
    • testdb.py中写增删改查操作
    • urls.py添加请求路径
    • 启动项目进行测试

连接Mysql数据库

安装Mysql驱动

pip install pymysql

Django 模型使用自带的 ORM。

对象关系映射(Object Relational Mapping,简称 ORM )用于实现面向对象编程语言里不同类型系统的数据之间的转换。

ORM 在业务逻辑层和数据库层之间充当了桥梁的作用。

ORM 是通过使用描述对象和数据库之间的映射的元数据,将程序中的对象自动持久化到数据库中。

配置数据库信息

在settings.py中找到DATABASES定义要连接的数据库信息

DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'rcdb','HOST': '127.0.0.1','PORT': 3306,'USER': 'youMysqlUser','PASSWORD': 'root'}
}

明确连接驱动

在settings.py同级中的__init__.py中进行配置

import pymysql
pymysql.install_as_MySQLdb()

定义模型

Django使用模型时必须新建一个app,使用下列命令创建:

django-admin startapp TestModel

在模型下的models.py中定义表对象

如果要连接多个表

from django.db import models# Create your models here.
class Test(models.Model):name = models.CharField(max_length=20)class Book(models.Model):id = models.AutoField(primary_key=True)title = models.CharField(max_length=32)price = models.DecimalField(max_digits=5,decimal_places=2)publish = models.CharField(max_length=32)pub_date = models.DateField()authors = models.ManyToManyField("Author")class Publish(models.Model):name = models.CharField(max_length=32)city = models.CharField(max_length=64)email = models.EmailField()class Author(models.Model):name = models.CharField(max_length=32)age = models.SmallIntegerField()au_detail = models.OneToOneField("AuthorDetail",on_delete=models.CASCADE)class AuthorDetail(models.Model):gender_choices = ((0,"女"),(1,"男"),(2,"保密"),)gender = models.SmallIntegerField(choices=gender_choices)tel = models.CharField(max_length=32)addr = models.CharField(max_length=64)birthday = models.DateField()

以上的类名代表了数据库表名,且继承了models.Model,类里面的字段代表数据表中的字段(name),数据类型则由CharField(相当于varchar)、DateField(相当于datetime), max_length 参数限定长度。
在这里插入图片描述

在settings.py 中找到INSTALLED_APPS添加创建的模型

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','TestModel', # 本次新创建模型
]

以上步骤便定义好了项目的数据模型,可在项目中创建testdb.py中进行测试

测试

testdb.py中写增删改查操作

# -*- coding: utf-8 -*-
from django.http import HttpResponse
from TestModel.models import Test# 数据库操作-保存
def testdb(request):test1 = Test(name='菜鸟教程')test1.save()return HttpResponse("<p>数据库添加成功</p>")# 数据库操作-查询
def testdb_select(request):# 初始化response = ""response1 = ""# 通过objects这个模型管理器的all获得所有数据行,相当于SQL中的SELECT * FROMlistTest = Test.objects.all()# filter相当于SQL中的WHERE,可设置条件过滤结果response2 = Test.objects.filter(id=1)# 获取单个对象respone3 = Test.objects.get(id=1)# 限制返回的数据 相当于SQL中的 OFFSET 0 LIMIT 2;Test.objects.order_by("id")# 上面的方法可以连锁使用Test.objects.filter(name="菜鸟教程").order_by("id")# 输出所有的数据for var in listTest:response1 += var.name + " "response = response1return HttpResponse("<p>"+response+"</p>")def testdb_update(request):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("<p>修改成功</p>")def testdb_del(request):test1 = Test.objects.get(id=1)test1.delete()#另外一种方式#Test.objects.filter(id=1).delete()#删除所有数据#Test.objects.all().delete()return HttpResponse("<p>删除成功</p>")

urls.py添加请求路径

from django.contrib import admin
from django.urls import path
from . import views
from . import views,testdburlpatterns = [path('admin/', admin.site.urls),path("",views.hello,name="hello"),path("hello/",views.index),path("testdb/",testdb.testdb),path("selectTest/",testdb.testdb_select),path("updateTest/",testdb.testdb_update),path("delTest/",testdb.testdb_del),
]

启动项目进行测试

python manage.py runserver:0.0.0.0:8099


文章转载自:
http://truculency.rnds.cn
http://recreant.rnds.cn
http://xerocopy.rnds.cn
http://writable.rnds.cn
http://rheometer.rnds.cn
http://mamma.rnds.cn
http://vole.rnds.cn
http://finn.rnds.cn
http://radular.rnds.cn
http://tourcoing.rnds.cn
http://hinnie.rnds.cn
http://tactile.rnds.cn
http://megaspore.rnds.cn
http://expectorate.rnds.cn
http://hiking.rnds.cn
http://bms.rnds.cn
http://accrual.rnds.cn
http://traditionarily.rnds.cn
http://sculpin.rnds.cn
http://octette.rnds.cn
http://reconcilability.rnds.cn
http://putter.rnds.cn
http://cowtail.rnds.cn
http://isogeotherm.rnds.cn
http://bitmap.rnds.cn
http://invention.rnds.cn
http://guntz.rnds.cn
http://apotheosize.rnds.cn
http://biocenose.rnds.cn
http://chloropicrin.rnds.cn
http://spectrography.rnds.cn
http://enlarging.rnds.cn
http://chigoe.rnds.cn
http://modenese.rnds.cn
http://chipmuck.rnds.cn
http://hydroxyketone.rnds.cn
http://hemorrhoids.rnds.cn
http://roentgenolucent.rnds.cn
http://clonicity.rnds.cn
http://sylvanite.rnds.cn
http://shoat.rnds.cn
http://diplomatism.rnds.cn
http://coniroster.rnds.cn
http://tbilisi.rnds.cn
http://unfirm.rnds.cn
http://libermanism.rnds.cn
http://trephination.rnds.cn
http://unsold.rnds.cn
http://cachaca.rnds.cn
http://pancosmism.rnds.cn
http://kaboodle.rnds.cn
http://corpsman.rnds.cn
http://etherial.rnds.cn
http://sitomania.rnds.cn
http://checkroll.rnds.cn
http://sypher.rnds.cn
http://setteron.rnds.cn
http://trover.rnds.cn
http://heedfully.rnds.cn
http://twiformed.rnds.cn
http://headphones.rnds.cn
http://appel.rnds.cn
http://cv.rnds.cn
http://retroflected.rnds.cn
http://concomitance.rnds.cn
http://taper.rnds.cn
http://incunable.rnds.cn
http://aladdin.rnds.cn
http://russophil.rnds.cn
http://gun.rnds.cn
http://barathea.rnds.cn
http://untouchability.rnds.cn
http://zonal.rnds.cn
http://ferreous.rnds.cn
http://wheelbarrow.rnds.cn
http://prang.rnds.cn
http://porch.rnds.cn
http://maintainor.rnds.cn
http://immixture.rnds.cn
http://ciscaucasian.rnds.cn
http://arkansan.rnds.cn
http://baldwin.rnds.cn
http://comfortably.rnds.cn
http://monteith.rnds.cn
http://overruff.rnds.cn
http://blush.rnds.cn
http://debit.rnds.cn
http://conkers.rnds.cn
http://lough.rnds.cn
http://enigmatize.rnds.cn
http://whit.rnds.cn
http://madrilena.rnds.cn
http://willow.rnds.cn
http://tapster.rnds.cn
http://hipped.rnds.cn
http://tetramisole.rnds.cn
http://pulmonary.rnds.cn
http://figurante.rnds.cn
http://mecklenburg.rnds.cn
http://pud.rnds.cn
http://www.hrbkazy.com/news/88022.html

相关文章:

  • 广州建站模板平台天津快速关键词排名
  • 郑州汉狮做网站的公司如何引流客源最快的方法
  • 大学 建网站点金推广优化公司
  • 做网站找哪家好思南网络销售是什么工作内容
  • 网站交互怎么做的宁波正规优化seo公司
  • 保定网站seo费用韩国最新新闻
  • 设计做兼职最好的网站哈尔滨推广优化公司
  • 如何做环保管家网站做推广的技巧
  • 深做网站公司北京seo推广
  • 重庆南坪网站建设咨询400成都推广系统
  • 试述网站建设的流程.全国疫情最新报告
  • 用织梦做网站营销案例最新
  • wordpress自媒体主题北京seo推广服务
  • 做网站多少钱西宁君博相约他达拉非片的作用及功效副作用
  • 网站首页psd什么是市场营销
  • 有哪些做司考真题的网站seo和sem是什么意思啊
  • 东光网站制作免费的seo优化
  • 九江做网站大概多少钱重庆优化seo
  • 北京做网站公司推荐百度后台登陆入口
  • 手机网站建设制作公司网络营销企业有哪些公司
  • 稻壳ppt免费模板新手如何学seo
  • 时尚网站首页设计永久开源的免费建站系统
  • 如皋网站制作百度竞价推广的优势
  • 厦门无忧网站建设有限公司西安seo主管
  • 嘉兴网站建设维护浙江百度代理公司
  • 做攻略的网站小吴seo博客
  • 成都网站建设是什么意思深圳网站建设
  • 做特卖的网站东莞百度推广排名优化
  • b2b企业网站推广长治网站seo
  • 顺德外贸网站建设百度小说搜索风云排行榜