使用 django 模板创建可读的 html

发布于 2024-10-09 23:26:12 字数 300 浏览 3 评论 0原文

当使用 Django 进行 html 模板时,如何创建良好的 html 标记格式。

我正在尝试利用内容块。但内容块在不同的模板中显示为不同的缩进级别。如何让内容块显示缩进,就像有人手写 html 一样。

我对换行符也有同样的问题;我可以将模板中的所有块粉碎在一起。此时 html 看起来更好,但模板无法维护。

我想问题是如何使用 django 模板系统创建漂亮的 html 标记?

到目前为止,我对答案感到惊讶。我发现格式良好的 HTML 有助于编写相应的 CSS 和 JavaScript。以及让以后添加内容变得更容易。

When using Django for html templating how do I create good html markup formatting.

I am trying to make use of content blocks. But the content blocks show up at different levels of indentation in different templates. How do I get the content blocks to show indented like it would be if someone was to hand write the html.

I am having the same problem with newlines; I can smash all the blocks together in the template. At that point the html looks better, but the templates are unmaintainable.

I guess the question is how to you create pretty html markup with the django templating system?

I am surprised by the answers so far. I find that nicely formatted HTML aids in writing the corresponding CSS and JavaScript. As well as making it easier to add content later on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浮世清欢 2024-10-16 23:26:12

来自 Pyevolve 的 Christian S. Perone 正是您所寻找的。他使用中间件拦截HTTPResponse对象并通过BeautifulSoup运行它。

不过,我想您的网站性能会受到轻微影响。我建议在将此中间件部署到生产站点之前运行一些基准测试。

Christian S. Perone from Pyevolve has exactly what you're looking for. He uses middleware to intercept the HTTPResponse object and run it through BeautifulSoup.

I'd imagine that your site will suffer a slight performance hit, though. I recommend running some benchmarks before deploying this middleware to a production site.

手长情犹 2024-10-16 23:26:12

您可以像我一样重写 NodeList 的渲染方法。使用工作代码查看我的问题:

正确Django 模板中的缩进(无需猴子修补)?

You can override the NodeList's render method as I've done. See my question with working code:

Proper indentation in Django templates (without monkey-patching)?

终难愈 2024-10-16 23:26:12

我印象深刻的是你关心生成的 html - 我只担心模板的整洁和可读!但在回答你的问题时,你看过中间件吗?

http://docs.djangoproject.com/en/dev/topics/http/ middleware/

然后使用 Tidy python 模块重新格式化 html。

http://pypi.python.org/pypi/PythonTidy/

I'm impressed you care about the resulting html - I only worry about getting the templates tidy and readable! But in answer to your question, have you looked at middleware?

http://docs.djangoproject.com/en/dev/topics/http/middleware/

Then use the Tidy python module to reformat the html.

http://pypi.python.org/pypi/PythonTidy/

等待圉鍢 2024-10-16 23:26:12

我建议通过 HTML tidier 运行模板引擎的输出,例如 μTidylib

I recommend to run the output of the template engine through an HTML tidier, such as µTidylib.

绾颜 2024-10-16 23:26:12

HTML 的可读性并不重要。在 Django 或 Rails 等系统中,HTML 是一种输出格式,而不是源格式。当人们手动编辑 HTML 时,他们担心缩进和间距是正确的,因为它是一种源格式,将来必须有人编辑它。但模板的输出就是:输出。浏览器不关心缩进。此时,唯一阅读 HTML 的人是查看“查看 - 源代码”的人。

The readability of HTML doesn't matter. In a system like Django or Rails, HTML is an output format, not a source format. When people edit HTML by hand, they are right to be concerned about indentation and spacing, because it is a source format, and someone will have to edit it in the future. But the output of templates is just that: output. The browser doesn't care about the indentation. At this point the only people who read the HTML are people looking at View - Source.

嘿嘿嘿 2024-10-16 23:26:12

如果您使用 Django 模板对长而简单的字符串进行字符串格式化,则可以在函数 def 中使用带有 .format() 的 Python 三引号字符串:

def templated_string(context_dict):
    return '''This is an example
of a long string across multiple lines.
It can be used to format {something} for 
{name}'s own purposes, even though
it looks funny in code'''.format(**context_dict)

然后 templated_string({'name': ' rileymat', 'something': '无论你想要什么'}) 做了明智的事情。您可以通过这种方式生成原始 HTML...然后它就具有您所要求的空白。

If you're using Django Templates to do string-formatting of long-but-simple strings, you can use Python's triple-quoted strings with .format() in a function def:

def templated_string(context_dict):
    return '''This is an example
of a long string across multiple lines.
It can be used to format {something} for 
{name}'s own purposes, even though
it looks funny in code'''.format(**context_dict)

Then templated_string({'name': 'rileymat', 'something': 'whatever you want'}) does the sensible thing. You can generate raw HTML that way... which then has exactly the whitespace you ask for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文