在Django模型中存储和转义Django标签和过滤器 [英] Storing and escaping Django tags and filters in Django models

查看:130
本文介绍了在Django模型中存储和转义Django标签和过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将内容从我的模型输出到我的模板,但是一些模型字段调用存储在其他模型中的数据。这只发生在几个领域。我想知道如果使用如果标签来评估这将比在模型中存储django标签更有效率。

I am outputting content from my models to my templates, however some model fields call data stored in other models. This happens only in a few fields. I am wondering whether using an if tag to evaluate this would be more efficient compared to storing the django tags inside the models.

此问题说,在模型中存储django标签是一个坏主意,没有给出原因(虽然我认为其中一个原因可能是其他人可能会在数据库中注入一些标签)。假设数据库注入很稀少,有没有办法逃避存储在模型中的Django标签和过滤器。

Answers to this question say that storing django tags in models is a bad idea without giving reasons (although I think one of the reasons may be someone else may inject some tags in the Database). Assuming that database injection is a rarity, is there a way of escaping Django tags and filters stored in a model.

或者更好的是,最有效的方法是处理上述情况,其中一个模型字段在多个字段中调用存储在另一个模型中的字段。

Or better yet, what would be the most efficient method to handle the above situation where one model field in several fields calls fields stored in another model.

示例

这应该存储在我的模型中

This should be stored in my models

<p>We focus on:</p>
{% for item in services %}
{% url service_view item.id as service_url %}
<ul>
<li><a href="service_url">{{item.title}}</a></li>
</ul>
{% endfor %}

输出它应该导致django将相关的django标签解析为如果模板的一部分

Outputting it should result in django parsing the relevant django tags as if part of the template

推荐答案

感谢Ned,我尝试实现,但我发现它是相当复杂的,它也不利于可移植性条款

Thanks Ned, I tried implementing that but I found it to be quite complex and its also disadvantageous in terms of portability.

但是,我发现我在Django Snippets中需要的是什么(不知道为什么我先看不到)。它是一个非常有用的实用程序,称为 render_as_template

However, I found exactly what I needed at Django Snippets (dont know why I didn't look there first). Its a quite useful utility known as render_as_template.

将其设置为自定义标签后,我需要的是以 {%render_as_template about_view.content%} ,模型中的标签被渲染为模型。

After setting it up as a custom tag, all that I needed was to use it in the form {% render_as_template about_view.content %} and the tags in the models were rendered as models.

有关创建自己的自定义模板和标签的说明此处可用

Instructions on creating your own custom templates and tags available here

这篇关于在Django模型中存储和转义Django标签和过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆