Djiango项目从头来day03

2021-05-21  本文已影响0人  __method__

新建应用book

python manage.py startapp book

在settings.py注册应用



在book app 的model中编辑ORM类

from django.db import models

#定义图书模型类BookInfo
class BookInfo(models.Model):
    btitle = models.CharField(max_length=20)#图书名称
    bpub_date = models.DateField()#发布日期
    bread = models.IntegerField(default=0)#阅读量
    bcomment = models.IntegerField(default=0)#评论量
    isDelete = models.BooleanField(default=False)#逻辑删除
    class Meta:
        verbose_name = "图书信息"
        verbose_name_plural = verbose_name
        db_table = "bookinfo"

#定义英雄模型类HeroInfo
class HeroInfo(models.Model):
    hname = models.CharField(max_length=20)#英雄姓名
    hgender = models.BooleanField(default=True)#英雄性别
    isDelete = models.BooleanField(default=False)#逻辑删除
    hcomment = models.CharField(max_length=200)#英雄描述信息
    # on_delete=models.CASCADE 这就是默认的选项,级联删除,你无需显性指定它
    hbook = models.ForeignKey('BookInfo',on_delete=models.CASCADE)
    #英雄与图书表的关系为一对多,所以属性定义在英雄模型类中

    class Meta:
        verbose_name = "英雄信息"
        verbose_name_plural = verbose_name
        db_table = "heroinfo"

迁移数据



插入数据

insert into bookinfo(btitle,bpub_date,bread,bcomment,isDelete) values
('射雕英雄传','1980-5-1',12,34,0),
('天龙八部','1986-7-24',36,40,0),
('笑傲江湖','1995-12-24',20,80,0),
('雪山飞狐','1987-11-11',58,24,0);
insert into heroinfo(hname,hgender,hbook_id,hcomment,isDelete) values
('郭靖',1,1,'降龙十八掌',0),
('黄蓉',0,1,'打狗棍法',0),
('黄药师',1,1,'弹指神通',0),
('欧阳锋',1,1,'蛤蟆功',0),
('梅超风',0,1,'九阴白骨爪',0),
('乔峰',1,2,'降龙十八掌',0),
('段誉',1,2,'六脉神剑',0),
('虚竹',1,2,'天山六阳掌',0),
('王语嫣',0,2,'神仙姐姐',0),
('令狐冲',1,3,'独孤九剑',0),
('任盈盈',0,3,'弹琴',0),
('岳不群',1,3,'华山剑法',0),
('东方不败',0,3,'葵花宝典',0),
('胡斐',1,4,'胡家刀法',0),
('苗若兰',0,4,'黄衣',0),
('程灵素',0,4,'医术',0),
('袁紫衣',0,4,'六合拳',0);

编写视图函数

from django.shortcuts import render
from book.models import BookInfo, HeroInfo
# Create your views here.
def index(request):
    list = BookInfo.objects.all()
    return render(request, 'book/index.html', {'list':list})

配置路由

from django.contrib import admin
from django.urls import path
from demo.views import show
from book.views import index
urlpatterns = [
    path('admin/', admin.site.urls),
    path('demo/', show),
    path('book/', index),

编写前端页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>欢迎来到终点小说网</h1>
<ul>
    {% for book in list %}
        <li>{{ book.btitle }}</li>
    {% endfor %}

</ul>

<h1>英雄展示</h1>

</body>
</html>
from django.contrib import admin
from django.urls import path
from demo.views import show
from book.views import index, delete
from django.conf.urls import url
urlpatterns = [
    url(r'^delete/(\d+)/$', delete),

    path('admin/', admin.site.urls),
    path('demo/', show),
    path('book/', index),

]

编写视图函数

from django.shortcuts import render, redirect
def delete(request, id):
    print("delete 被调用, id=", id)
    # 根据主键查询
    bookinfo = BookInfo.objects.get(id=int(id))
    print(bookinfo.btitle)
    # 删除
    bookinfo.delete()
    # 转发
    return redirect('/book/',)

然后点击就可以删除了

增加和修改

页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>欢迎来到终点小说网</h1>
<a href="/create/">增加一本小说</a>


<ul>
    {% for book in list %}
        <li>{{ book.btitle }}------<a href="/delete/{{ book.id }}">删除</a>
            ------<a href="/update/{{ book.id }}">修改阅读量为50等信息</a>
</li>
    {% endfor %}

</ul>

<h1>英雄展示</h1>

</body>
</html>

路由


from django.contrib import admin
from django.urls import path
from demo.views import show
from book.views import index, create, delete, update
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    path('demo/', show),
    path('book/', index),
    path('create/', create),
    url(r'^delete/(\d+)/$', delete),
    url(r'^update/(\d+)/$', update),

]
def create(request):
    book = BookInfo()
    book.btitle = "小李飞刀"
    book.bcomment = 2222
    book.bpub_date = date(1999, 1, 19)
    book.save()
    return redirect("/book")
def delete(request, id):
    print("delete 被调用, id=", id)
    # 根据主键查询
    bookinfo = BookInfo.objects.get(id=int(id))
    print(bookinfo.btitle)
    # 删除
    bookinfo.delete()
    # 转发
    return redirect('/book/')


def update(request, id):
    bookinfo = BookInfo.objects.get(id=int(id))
    bookinfo.btitle = "侠客行"
    bookinfo.bcomment = 50
    bookinfo.save()
    return  redirect('/book/')

后台管理

本地化

LANGUAGE_CODE = 'zh-hans'# 中国语言

TIME_ZONE = 'Asia/Shanghai' # 时区

创建超级管理员

python manage.py createsuperuser

注册模型类

from django.contrib import admin

# Register your models here.
from book.models import BookInfo, HeroInfo

admin.site.register(BookInfo)
admin.site.register(HeroInfo)

重新模型类str方法

    def __str__(self):
        return self.btitle
上一篇下一篇

猜你喜欢

热点阅读