• Django框架 models模块使用


     

      

       #启动数据库(sqlite3)

     dos下:

      

      python manage.py makemigrations

      python manage.py migrate

     #views 

    from django.shortcuts import render
    from apppp.models import Book
    from django.http import HttpResponse
    # Create your views here.
    def goindex(request):
    return render(request,'index.html')


    #添加数据
    def addBook(request):
    if request.POST:
    #接收数据
    name=request.POST.get('bname')
    #写入数据库
    book=Book(bname=name)
    book.save()
    return HttpResponse('<script>alert("添加成功")</script>')
    else:
    return render(request,'insert.html')

    #查询数据
    def chaxun(request):
    #查询数据
    res=Book.objects.all()
    content={}
    content['chaxunkey']=res
    return render(request,'chaxun.html',content)

    #根据指定字段查询数据
    #根据编号查询
    def NOchaxun(request):
    #接收数据
    id=request.GET.get('id')
    #查询数据
    res=Book.objects.get(bid=id)
    content={}
    content['NOchaxunkey']=res
    return render(request,'NOchaxun.html',content)

    #删除业务(根据条件)
    def deletedemo(request,id):
    #删除数据
    Book.objects.filter(bid=id).delete()
    return HttpResponse('<script>alert("删除成功")</script>')

    #修改
    def xiugai(request,id,name):
    #修改数据 Book.objects.filter(bid=id).update(bname=name) return HttpResponse('<script>alert("修改成功")</script>')




    #models  
    from django.db import models

    # Create your models here.
    #继承models
    class Book(models.Model):
    #设置数据表字段
    bid=models.AutoField(primary_key=True)
    bname=models.CharField(max_length=30)


    #urls
    from django.contrib import admin
    from django.urls import path
    from apppp.views import goindex,addBook,chaxun,NOchaxun,deletedemo,xiugai

    urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',goindex),
    path('addBook/',addBook),
    path('chaxun/',chaxun),
    path('NOchaxun/',NOchaxun),
    path('deletedemo/<int:id>/',deletedemo),
    path('xiugai/<int:id>/<str:name>/',xiugai),
    ]
    
    
    #index.html
      
    
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <h3>[1]数据库添加操作</h3>
    <a href="/addBook/">添加</a>

    <h3>[2]数据库查询操作</h3>
    <a href="/chaxun/">查询</a>

    <h3>[3]数据库条件查询操作</h3>
    <a href="/NOchaxun/?id=4">条件查询</a>

    <h3>[4]数据库条件删除操作</h3>
    <a href="/deletedemo/1/">条件删除</a>

    <h3>[5]数据库条件修改操作</h3>
    <a href="/xiugai/8/中软卓越/">条件修改</a>
    </body>
    </html>

    #insert.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <form method="post" action="/addBook/">
    {% csrf_token %}
    <input type="text" name="bname">
    <button type="submit">添加</button>
    </form>
    </body>
    </html>

    #chaxun.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>chaxun</title>
    </head>
    <body>
    {% for foo in chaxunkey %}
    {{ foo.bid }}-{{ foo.bname }}<br/>
    {% endfor %}
    <br/>
    <table border="1">
    <tr>
    <td>编号</td>
    <td>名称</td>
    </tr>
    {% for foo in chaxunkey %}
    <tr>
    <td>{{ foo.bid }}</td>
    <td>{{ foo.bname }}</td>
    </tr>
    {% empty %}
    <tr>
    <td colspan="2">暂无数据</td>
    </tr>
    {% endfor %}

    </table>
    </body>
    </html>

    #NOchaxun.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>按编号查询</title>
    </head>
    <body>
    <h2>{{ NOchaxunkey }}</h2>
    数据1:{{ NOchaxunkey.bid }}<br/>
    数据2:{{ NOchaxunkey.bname }}
    </body>
    </html>
     

     











  • 相关阅读:
    Java实现 LeetCode 715 Range 模块(选范围)
    HTML 图像
    HTML 样式- CSS
    HTML <head>
    HTML 链接
    HTML 文本格式化
    目标检测中的anchor-based 和anchor free
    目标检测coco数据集点滴介绍
    Camera HDR Algorithms
    噪声标签的负训练:ICCV2019论文解析
  • 原文地址:https://www.cnblogs.com/zhouqi245776620/p/10038681.html
Copyright © 2020-2023  润新知