使用 django 在文件中使用标记

发布于 2024-09-07 10:19:32 字数 1538 浏览 6 评论 0原文

我目前正在使用 django 开发一个网站,我的问题是该网站必须从使用 php 脚本移植到使用 django。 虽然网站内容已经被以前的维护者维护得很好,但我必须对已经包含大量内容的文件使用 markdown,例如主页被分为目录中的三个文件,例如 a.html、b .html、c.html 虽然它们都包含简单的文本内容,但我是否必须单独渲染它们,我应该使用 view.py 文件来解析结构还是使用模板来解析结构,真正的问题是如何解析模板内文件的内容

我编写​​了一个模板:

{% extends "catalog.html" %}

{% block content %}

<div class="yui-g" id="masthead">
<div id="main_feature">
    <div id="main_feature_content">
    {% include "features/main.html" %}
    </div>
</div>
</div>

<div class="yui-g" id="main_information">
<div class="yui-g" style="float: left; width: 49%">
{% include "features/about.html" %}
</div>
<div class="yui-g" style="float: right; width: 49%">
    <h2>Recent Headlines</h2>
    <a href="/news">More</a>
</div>
</div>


<div class="yui-g" id="features_container">
<div id="features">
<div class="feature feature_developer">
    <div class="feature_content">
    {% include "features/1.html" %}
    </div>
</div>

<div class="feature feature_middle feature_community">
    <div class="feature_content">
    {% include "features/2.html" %}
    </div>
</div>

<div class="feature feature_community">
    <div class="feature_content">
    {% include "features/3.html" %}
    </div>
</div>
</div>
</div>


{% endblock %}

这是我需要解析的包含文件:- {% include "features/about.html" %} 但任何人都会明白这只会显示文件内容而不是解析的 html。感谢您提前的帮助

I am currently working on a website using, django, my problem is that the site has to be ported from using php scripts to using django.
Though the site content has been well maintained by the previous maintainer, I have to use markdown for files that already having a HUGE amount of content in them, like the main page is divided into three files inside a directory, like a.html, b.html, c.html though they all contain simple text content, do I have to render them all seperately, should i use the view.py file for parsing the structure or use a template for the same, the real question is how to parse the contents of a file INSIDE the template

I wrote a template:

{% extends "catalog.html" %}

{% block content %}

<div class="yui-g" id="masthead">
<div id="main_feature">
    <div id="main_feature_content">
    {% include "features/main.html" %}
    </div>
</div>
</div>

<div class="yui-g" id="main_information">
<div class="yui-g" style="float: left; width: 49%">
{% include "features/about.html" %}
</div>
<div class="yui-g" style="float: right; width: 49%">
    <h2>Recent Headlines</h2>
    <a href="/news">More</a>
</div>
</div>


<div class="yui-g" id="features_container">
<div id="features">
<div class="feature feature_developer">
    <div class="feature_content">
    {% include "features/1.html" %}
    </div>
</div>

<div class="feature feature_middle feature_community">
    <div class="feature_content">
    {% include "features/2.html" %}
    </div>
</div>

<div class="feature feature_community">
    <div class="feature_content">
    {% include "features/3.html" %}
    </div>
</div>
</div>
</div>


{% endblock %}

this is the included file i needed to parse :- {% include "features/about.html" %}
but anyone will understand that this will only display the file contents not the parsed html. Thanks for the help in advance

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

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

发布评论

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

评论(1

祁梦 2024-09-14 10:19:32

{% include %} 标签仅处理 Django 模板文件,不支持任何自定义处理,例如处理 markdown 本身。您有几种选择:

  • 您可以使用 {% load markup %}{% filter markdown %}{% endfilter %} 将所有 Markdown 内容包装在包含的模板中>。 过滤器标签将指定的过滤器应用于它的内容。您将需要在每个模板的开头添加 {% load markup %} 行,因为每个模板都需要加载它使用的附加标记库。
  • 您的视图可以加载内容并将其作为上下文变量提供,以便您可以在模板中执行类似 {{ aboutcontent|markdown }} 的操作(其中 aboutcontent 是您的视图提供的上下文变量)。
    • 您的视图还可以为您进行降价转换,因此您只需执行 {{ aboutcontent }}
  • 您可以编写一个自定义模板标签来为您执行加载和降价转换,但这要复杂得多,您可能最好使用其他选项之一,或者简单地重新考虑和更新模板以不需要此处理。

The {% include %} tag only processes Django template files, and does not support any custom processing such as handling markdown in and of itself. You have a few options:

  • You can wrap all of the markdown content in the included templates with {% load markup %}{% filter markdown %} and {% endfilter %}. The filter tag applies the specified filter(s) to its contents. You will need the {% load markup %} line in the beginning of each template, as each template needs to load the additional tag libraries that it uses.
  • Your view can do the loading of the content and provide it as a context variable, so that you can do something like {{ aboutcontent|markdown }} in your template (where aboutcontent is the context variable your view provided).
    • Your view could also do the markdown conversion for you, so you'd simply have to do {{ aboutcontent }}.
  • You could write a custom templatetag that does the loading and markdown conversion for you, but that's far more complicated and you'd probably be better off with one of the other options, or simply rethinking and updating your templates to not require this processing.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文