美文网首页
6、将数据库中数据在前台显示

6、将数据库中数据在前台显示

作者: 测试星云 | 来源:发表于2022-05-08 09:18 被阅读0次

1、将后端数据传递回前端界面(常用格式列表字典)

views.py中Render中参数context传递动态数据

rooms=[ {'name':'python web 技术'},{'name':'测试技术'}, {'name':'人工智能'}]
def hello(request):
    context={'rooms':rooms}
    return render(request,'hello.html',context)

2、前端界面循环显示出来

在html页面中添加 :变量调用: {{变量名}} 代码使用{% %}

{% for room in rooms %}
        <h5> {{room.name}}</h5>
{% endfor %}

3、将数据库的数据取回到后端中

获取这个ORM模型的QuerySet对象。即获取所有的数据。
模型中类名.objects.all()

from .models import Category

context = {'rooms': Category.objects.all()}

4、可使用bootstrap写好的css样式变好看

  • https://getbootstrap.com/

  • https://getbootstrap.com/docs/5.1/examples/

  • head中加上css(也可使用下载到本地的)
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

  • 将源代码复制到html中。可直接使用。


    image.png

相关文章

网友评论

      本文标题:6、将数据库中数据在前台显示

      本文链接:https://www.haomeiwen.com/subject/joofurtx.html