python生成html文档,在python中生成HTML文档

我建议使用Python提供的许多模板语言之一,例如一个built into Django(你没有使用Django的其余部分来使用它的模板引擎) - 谷歌查询应该给你很多其他的替代模板实现。

我发现学习模板库有很多方面的帮助 - 无论何时您需要生成电子邮件,HTML页面,文本文件或类似文件,您只需编写一个模板,将其加载到模板库中,然后让模板代码创建成品。

下面是一些简单的代码,让你开始:

#!/usr/bin/env python

from django.template import Template, Context

from django.conf import settings

settings.configure() # We have to do this to use django templates standalone - see

# http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django

# Our template. Could just as easily be stored in a separate file

template = """

Template {{ title }}

Body with {{ mystring }}.

"""

t = Template(template)

c = Context({"title": "title from code",

"mystring":"string from code"})

print t.render(c)

,如果你有在磁盘上的模板这是更简单 - 检查出render_to_string功能,可以从搜索路径预设列表从磁盘加载模板,填写数据从一个胜利和呈现到一个字符串 - 所有在一个函数调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值