User-Profile-Image
hankin
  • 5
  • centos7
  • docker
  • mysql
  • PostgreSQL
  • git/gitlab
  • ELK
  • python
    • python-Tornado
    • python-django
  • redis
  • nginx
  • kvm
  • proxmox
  • mongo
  • kubernetes
  • prometheus
  • GlusterFs
  • nfs
  • freeswitch
  • httpd
  • shell脚本
  • linux
  • fastdfs
  • nextcloud
  • openssl
  • openvpn
  • rabbitmq
  • sqlite
  • svn
  • java
  • ubuntu
  • vue2
  • wordpress
  • php
  • IOT物联网
  • 项目
  • 故障处理
  • 树莓派
  • 博客存档
  • 未分类
  • 杂项
  • #1742(无标题)
  • 新视野
  • 分类
    • 项目
    • 树莓派
    • 杂项
    • 未分类
    • 新视野
    • 故障处理
    • 博客存档
    • 交换机
    • wordpress
    • vue2
    • ubuntu
    • svn
    • sqlite
    • shell脚本
    • redis
    • rabbitmq
    • python-django
    • python
    • proxmox
    • prometheus
    • PostgreSQL
    • php
    • openvpn
    • openssl
    • nginx
    • nfs
    • nextcloud
    • mysql
    • mongo
    • linux
    • kvm
    • kubernetes
    • java
    • IOT物联网
    • httpd
    • GlusterFs
    • git/gitlab
    • freeswitch
    • fastdfs
    • ELK
    • docker
    • centos7
  • 页面
    • #1742(无标题)
  • 友链
      请到[后台->主题设置->友情链接]中设置。
Help?

Please contact us on our email for need any support

Support
    首页   ›   python-django   ›   正文
python-django

django前后端不分离入门

2022-10-28 23:33:23
755  0 0

在django中创建html模板,关联数据,返回给浏览器

修改settings.py文件
将 'DIRS': [] 修改为'DIRS': [os.path.join(BASE_DIR, 'templates')],
项目目录下创建模板文件夹templates

templates下创建模板文件index.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>

    <body class="globcss">
        <table border="0" class="tablecss">  <tr>  <td class="keycss">{{key0}}</td> <td class="valuecss">&emsp;{{value0}}</td>  </tr>    </table>
        <div class="line"></div>

        <table border="0" class="tablecss">  <tr>  <td class="keycss">{{key10}}</td> <td class="valuecss">&emsp;{{value10}}</td>  </tr>    </table>
        <div class="line"></div>
    </body>
</html>

<style>
    .globcss {
        font-size: 36px;
        line-height: 2em;
        width: 100%;
        height: 100%;
        background-color: #f3f4f6;

    }
    .tablecss {
                width: 100%;
    }
    .keycss {
        color: green;
        width: 18%;
        text-align-last: justify;
    }
    .valuecss {
        color:  #000000;
    }
    .trcss {
           margin-top: 100px;
    }
    .div1{

    }

    .line{
        width: 100%;
        border-bottom: 1px solid #000000;
        color:green;
    }
    .test{
    background-color: #a3f4f6;
    }

</style>
views.py编写
from django.shortcuts import render
from django.http import HttpResponse
from django.core import serializers

def get_data(request):
    ...
    return render(request, "index.html",{"key0": "11", "value0": "22"})
    #return render(request, "err.html", {"err": "404"})
urls.py指定路由
...
浏览器访问测试
#html文件有修改,保存并刷新浏览器即可显示最新内容。

其他

https://www.cnblogs.com/my_life/articles/8385635.html
其他
Django 模板标签     
    变量 模板语法:    
    view:{"HTML变量名" : "views变量名"}   
    HTML:{{变量名}}    
html中使用变量方法     
    1   <h1>{{ str_data }}</h1>
    2   <p>{{ views_list }}</p> # 取出整个列表
    3   <p>{{ views_list.0 }}</p> # 取出列表的第一个元素
    4   <p>{{ views_dict.name }}</p> 用 .键 取出对应的值
评论 (0)

点击这里取消回复。

欢迎您 游客  

Copyright © 2025 网站备案号 : 蜀ICP备2022017747号
smarty_hankin 主题. Designed by hankin
主页
页面
  • #1742(无标题)
博主
tang.show
tang.show 管理员
linux、centos、docker 、k8s、mysql等技术相关的总结文档
213 文章 2 评论 202080 浏览
测试
测试