python - 在 Django 中显示漂亮的代码

标签 python html django

我正在寻找可以在 Django 中使用的东西来显示预先格式化的代码。理想情况下,这将包括针对各种编程语言的开箱即用的语法突出显示,尽管从能够很好地显示 html 和 xml 的东西开始将是一个很好的起点。

这样的东西存在吗?

基本上,我正在寻找用于显示代码的小部件 dpaste(以及堆栈溢出)之类的东西。

例如http://dpaste.com/hold/102141/

<?xml version="1.0" encoding='UTF-8'?>
<painting>
  <img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
  <caption>This is Raphael's "Foligno" Madonna, painted in
  <date>1511</date>-<date>1512</date>.</caption>
</painting>

我知道 this question ,但我的不是关于转义代码的机制,而是关于 UI。

最佳答案

你可以使用 Pygments进行语法高亮并显示 HTML。

示例代码:

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

highlighted = highlight('# Some Python code', PythonLexer(), HtmlFormatter())

另见 official documentation .

关于python - 在 Django 中显示漂亮的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1514874/

相关文章:

javascript - 如何使 JavaScript 'getElementById' 在 HTML CSS 切换开关上工作

Django 过滤查询在大行数据集上速度缓慢

Django Admin 过滤多选

python - boto3 给出 AccessDenied,有没有办法查找缺少的权限?

python - SQLAlchemy IN 子句

javascript - 转义 HTML 值是什么意思?

python - Django 查询特殊字符

python - python中迭代语句后是否存在迭代变量?

Python 3 静态成员

javascript - 将第二个 div 的内容滚动到第一个 div 的末尾