zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    Linux配置java环境
    三级联动的实现
    Linux安装
    省市区县的sql语句——城市
    shiro登陆权限验证
    省市区县的sql语句——区县
    Linux安装Jenkins
    省市区县的sql语句——省
    读《世界是数字的》有感
    读《我是一只IT小小鸟》有感
  • 原文地址:https://www.cnblogs.com/feifang/p/6249285.html
Copyright © 2011-2022 走看看