django数据库的增删改查
正常启动服务
安装数据库
cmd命令:python manage.py makemigrations 创建用户 在pxm里面会有一个py文件..这就是
生成了表结构
cmd命令:python manage.py migrate 自带的一些表 导入
这就是正向导入数据库
反向导入数据库:python manage.py inspectdb > son1/models.py
倒过来的数据库可以吧没用的都删除掉
修改:list=Shop.objects.filter(id=2).update(shopname='ljl') 只在数据库显示,页面没有显示
删除:list=Shop.objects.all().delete()
增加:list=Shop.objects.create(shopname='liu')
查找:list=Shop.objects.filter(id=2).all() 查找:filter属于查找单条
list=Shop.objects.filter(id__gte=1).all().order_by('id') 排序
models.py
from son6.models import *
urls.py
from django.conf.urls import url
from django.contrib import admin
from son7.views import *
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/',index),
]
views.py
from son7.models import *
def index(request):
# list=Shop.objects.get(id=2) 得到id=2的数据
# list.shopname='liujianliang' 替换
# list.save()
# list=Shop.objects.filter(id=2).update(shopname='ljl') 修改
# list=Shop.objects.create(shopname='liu') 增加
list=Shop.objects.all().delete() 删除全部
return render(request,'index.html',locals())
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ list.shopname }}
{#{% for foo in list %}#}
{# {{ foo.id }}#}
{# {{ foo.shopname }}#}
{#{% endfor %}#}
</body>
</html>
models.py
from __future__ import unicode_literals
from django.db import models
class Shop(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
shopname = models.CharField(unique=True, max_length=20, blank=True, null=True)
class Users(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
username = models.CharField(max_length=50)
pwd = models.CharField(max_length=20)