• 增加、修改、删除


    第一;,models.py建表

    from django.db import models
    
    # Create your models here.
    
    class Book(models.Model): #必须继承models.Model这个类,jango才能识别Book是一张表
        name=models.CharField(max_length=20)#charfield字符串类型,长度20
        #price=models.FloatField()
        price=models.IntegerField ()
        pub_date=models.DateField()
        author=models.CharField(max_length=32,null=False)
    
    class Author(models.Model):
        name=models.CharField(max_length=32)

    第二 urls.py 建立连接

    from django.conf.urls import url
    from django.contrib import admin
    
    from app import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'index',views.index),
        url(r'addbook',views.addbook),
        url(r'update',views.update),
        url(r'delete',views.delete),
    
    ]

    第三:views.py 写处理函数

    from django.shortcuts import render,HttpResponse
    from app.models import *
    
    # Create your views here.
    def index(request):
        return render(request,"index.html")
    def addbook(request):
        #Book,使用导入from app.models import *
        b=Book(name="java",price=99,author="qiqi",pub_date="1456-12-12")
        b.save()#save执行,则上面语句成功
        #Book.objects.create(name="",。。。)另一种添加数据,不需要save
        return HttpResponse("添加成功")
        #要想此句需要from django.shortcuts import render,HttpResponse
    def update(request):
        Book.objects.filter(name="java").update(price="990")#修改方法一
    
        # #修改方法二
        # b=Book.objects.get(name="java")#拿到的b是列表类型是queryset集合的
        # b[0].price=100
        # b[0].save()
        return HttpResponse("修改成功")
    def delete(request):
        Book.objects.filter(name="java").delete()
        return HttpResponse("删除成功")

    输入python manage.py rnserver 8800 访问127.0.0.1:8800/index

    第四部:index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .head{
                line-height: 40px;
                background-color: green;
                color: white;
                text-align: center;
            }
    
        </style>
    </head>
    <body>
    <div class="outer">
        <div class="head">标题</div>
        <div class="content">
            <a href="/addbook/">添加书籍</a>
            <a href="delete">删除书籍</a>
            <a href="update">修改书籍</a>
        </div>
    </div>
    </body>
    </html>
  • 相关阅读:
    12.3、Libgdx的图像之截屏
    12.2、Libgdx的图像之清屏
    12.1、Libgdx的图像之持续性和非持续性渲染
    12、Libgdx的图像之全屏和垂直同步
    11.4、Libgdx的音频之录制PCM音效
    11.3、Libgdx的音频之播放PCM音频
    11.2、Libgdx的音频之音乐
    11.1、Libgdx的音频之音效
    11、Libgdx的音频
    阿里云服务器部署项目后台运行(入门)
  • 原文地址:https://www.cnblogs.com/wfl9310/p/9430124.html
Copyright © 2020-2023  润新知