zoukankan      html  css  js  c++  java
  • Django(五)1

    一、从数据库读取图书数据并渲染出来

    1)app1/views.py函数books编写

    【1】从模型下导入bookinfo信息
    【2】从数据库获取图书对象列表
    【3】把获取到的图书对象赋值给books键。【注意】键'books'必须要加引号

    from django.shortcuts import render
    from app1.models import BookInfo #【1】从模型下导入bookinfo信息
    
    # app1应用首页
    def index(request):
        context={} #定义1个字典
        context['hello']='hello world!!!' #向字典写一个键:值(hello:'hello world!!')
        context['wa']='wawawawawahahahaha!'
        context['list']=list(range(1,10)) #定义一个字典值为一个列表,list为把内容转换为列表
        return render(request,'app1/index.html',context) #返回:把context渲染到app1/index.html的模板文件
    
    # app1应用图书页
    def books(request):
        books=BookInfo.objects.all()#【2】从数据库获取图书对象列表
        return render(request,'app1/book.html',{'books':books})#【3】把获取到的图书对象赋值给books键。【注意】键'books'必须要加引号
    

    2)app1/urls.py配置

    【1】引入views
    【2】books应用配置

    from django.urls import path
    from . import views #【1】引入views
    
    urlpatterns=[
        path('app1/',views.index),
        path('books/',views.books),#【2】books应用配置
    ]
    

    3)templates/app1/book.html模析创建编写

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>书籍页面</title>
    </head>
    <body>
    本站的图书有:
    <ul>
        {% for book in books %}
        	<li>{{book.btitle}}:{{book.bpub_date}}</li>
        {%empty%}
        	暂时没有图书!!!
        {% endfor %}
    </ul>
    </body>
    </html>
    

    效果:http://127.0.0.1:8000/books/

    本站的图书有:
    天龙八部:1991年1月2日
    射雕英雄传:2020年1月2日

    二、通过url传参,访问书英雄详情

    1)加带参数的链接/templates/app1/book.html

    【1】通过href属性,把book的id做为参数传给详情页,进而查询对应英雄信息

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>书籍页面</title>
    </head>
    <body>
    本站的图书有:
    <ul>
        {% for book in books %}
    		<!--【1】通过href属性,把book的id做为参数传给详情页,进而查询对应英雄信息-->
       		<li><a href="/detail/{{book.id}}"> {{book.btitle}}</a>:{{book.bpub_date}}</li>
        {%empty%}
        	暂时没有图书!!!
        {% endfor %}
    </ul>
    </body>
    </html>
    

    2)接收参数,渲染到模板app1/views.py

    【1】查询主键为url中传过来的参数Id。或写成:id=bookId
    【2】关联查询:查询对应书的所有英雄信息
    【3】把参数渲染到detail页面去

    from django.shortcuts import render
    from app1.models import BookInfo #【0】从模型下导入bookinfo数据模型
    
    def index(request):
        '''app1应用:首页'''
        context={} #定义1个字典
        context['hello']='hello world!!!' #向字典写一个键:值(hello:'hello world!!')
        context['wa']='wawawawawahahahaha!'
        context['list']=list(range(1,10)) #定义一个字典值为一个列表,list为把内容转换为列表
        return render(request,'app1/index.html',context) #返回:把context渲染到app1/index.html的模板文件
    
    def books(request):
        '''app1应用:图书列表页'''
        books=BookInfo.objects.all()#从数据库获取图书对象列表
        return render(request,'app1/book.html',{'books':books})#把获取到的图书对象赋值给books键。【注意】键'books'必须要加引号
    
    def detail(request,bookId):# bookId为接收urls.py中指定的参数,来源页templates/app1/book.html
        '''app1应用:图书详情页,显示英雄信息'''
        book=BookInfo.objects.get(pk=bookId) #【1】查询主键为url中传过来的参数Id。或写成:id=bookId
        heros=book.heroinfo_set.all() #【2】关联查询:查询对应书的所有英雄信息
        return render(request,'app1/detail.html',{'book':book,'heros':heros}) #【3】把参数渲染到detail页面去
    

    3) 配置url及传参形式 app1/urls.py

    【书详情页】,通过url接收参数2种写法以下两种都可

    from django.urls import path,re_path
    from . import views
    
    urlpatterns=[
        path('app1/',views.index),
        path('books/',views.books),
    
        # 【书详情页】,通过url接收参数2种写法以下两种都可:
        # path(r"detail/<int:bookId>",views.detail), #参数用尖括号包起来<>
        re_path(r"^detail/(d+)",views.detail), #参数必须要带括号
        
    ]
    
    

    4)详情模板 templates/app1/detail.html

    【1】如果数据为空则执行如下代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>图书详情页,英雄信息</title>
    </head>
    <body>
    {{book.btitle}}的英雄有:
    <ul>
        {% for hero in heros %}
        	<li>{{hero.hname}}:{{hero.hcomment}}</li>
        {%empty%}<!--【1】如果数据为空则执行如下代码-->
        	<li>暂时没有英雄信息!!</li>
        {% endfor %}
    </ul>
    </body>
    </html>
    

    5.附件)project1/models.py

    表来源此处

    from django.db import models
    # 设计和表对应的类,模型类
    
    # 一类
    # 图书类
    class BookInfo(models.Model):
        '''图书模型类'''
        # 图书名称,CharField说明是一个字符串,max_length指定字符串的最大长度
        btitle = models.CharField(max_length=20)
        # 出版日期,DateField说明是一个日期类型
        bpub_date = models.DateField()
    
        def __str__(self): #重定义系统的str方法,让它返回对应图书的名字
            return self.btitle
    
    # 多类
    # 英雄人物类
    # 英雄名 hname
    # 性别 hgender
    # 年龄 hage
    # 备注 hcomment
    # 关系属性 hbook,建立图书类和英雄人物类之间的一对多关系
    class HeroInfo(models.Model):
        '''英雄人物模型类'''
        hname = models.CharField(max_length=20) # 英雄名称
        # 性别,BooleanField说明是bool类型,default指定默认值,False代表男
        hgender = models.BooleanField(default=False)
        # 备注
        hcomment = models.CharField(max_length=128)
        # 关系属性 hbook,建立图书类和英雄人物类之间的一对多关系
        # 关系属性对应的表的字段名格式: 关系属性名_id
        hbook = models.ForeignKey('BookInfo', on_delete=models.CASCADE) #对应BookInfo表的主键ID
    
        def __str__(self): #返回英雄名
            return self.hname
    

    效果:点书列表 http://127.0.0.1:8000/books/

    在这里插入图片描述

    进入英雄详情:http://127.0.0.1:8000/detail/3

    在这里插入图片描述

    英雄为空,在localhost:8000/admin新加一个书《笑傲江湖》,但不要加英雄信息,book页再点进去后:http://127.0.0.1:8000/detail/4

    在这里插入图片描述

  • 相关阅读:
    JAVA类与对象(十)-----抽象类
    JAVA类与对象(九)------多态
    JAVA类与对象(八)-----重写
    JAVA类与对象(七)------继承
    Mysql与Oracle区别
    redis缓存技术学习
    关于java中B/S架构
    关于java中C/S架构,创建服务器和客户端
    JQuery 轮播图片
    Kendo中ListView 无分页控件显示和有分页控件显示
  • 原文地址:https://www.cnblogs.com/chenxi188/p/12176655.html
Copyright © 2011-2022 走看看