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', {})
  • 相关阅读:
    pymysql学习笔记
    logging库基本使用(官方文档搬运)
    ddt学习笔记
    openpyxl 基本使用
    jieba库基本使用
    PyInstaller库基本使用
    time库基本使用
    react-dnd 拖拽排序
    create-react-app 基于ts项目,使用react-router-dom搭建项目
    使用create-react-app创建ts项目
  • 原文地址:https://www.cnblogs.com/exclm/p/3343707.html
Copyright © 2011-2022 走看看