zoukankan      html  css  js  c++  java
  • django: template

    本文接 django: startproject

    方法一:

    1 创建模板文件

    在 project_name/blog 下创建模板目录 templates:

    # cd project_name/blog
    # mkdir templates

    templates 目录中创建模板文件 index.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Django Template</title>
            <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
            <meta name="keywords" content="Django Template" />
            <meta name="description" content="Django Template" />
        </head>
        <body>
            <center>Django Template Learning.</center>
        </body>
    </html>
    View Code

    2 在视图中发布模板

    修改 blog/views.py:

    from django.http import HttpResponse
    from django.template import loader,Context
    
    def index(req):
            t = loader.get_template('index.html')   # auto load file 'index.html' from dir templates
            c = Context({})
            return HttpResponse(t.render(c))

    方法二:

    1 使用 render_to_response() 函数

    加载模板、初使化 Context 容器、返回HttpResponse 重复性大,可以直接使用 render_to_response 函数简化:

    blog/views.py:

    from django.shortcuts import render_to_response
    
    def index(req):
            return render_to_response('index.html', {})
  • 相关阅读:
    调试导论
    CSP-S2 2020 游记
    【题解】51nod 1327 棋盘游戏
    基础数学专题复习
    ubuntu 下 zsh 插件及安装方式
    ubuntu 下 Deepin-TIM 折腾笔记
    微服务学习笔记
    使用 Portainer 管理 Docker 笔记(含本地和远程)
    博客收藏
    定制unittest测试报告【转】
  • 原文地址:https://www.cnblogs.com/exclm/p/3343707.html
Copyright © 2011-2022 走看看