英文:
How to use safe filter on custom Django tag?
问题
我正在尝试实现一个自定义的Django标签,该标签将在Javascript中制定一个导入语句,以使用axios的get请求加载我的vue3应用以及其组件模板HTML文件。
我的“templatetags”目录中的自定义标签如下:
`templatetags/vuecomponents.py`
from django import template
from django.templatetags.static import static
register = template.Library()
@register.simple_tag
def v_load_app_component(app_name, script_name, components):
components = components.strip("[]").split(", ")
app_script = static(f"js/{script_name}.js")
comps = [static(f"components/{name}.html") for name in components]
return f"import {{{ app_name }}} from \"{app_script}?{components[0]}={comps[0]}\""
英文:
I'm trying to implement a custom Django tag that will formulate an import statement in Javascript to load my vue3 app as well as its components template html files using a get request in axios.
The custom tag in my templatetags
directory looks like this:
templatetags/vuecomponents.py
from django import template
from django.templatetags.static import static
register = template.Library()
@register.simple_tag
def v_load_app_component(app_name, script_name, components):
components = components.strip("[]").split(", ")
app_script = static(f"js/{script_name}.js")
comps = [static(f"components/{name}.html") for name in components]
return f"import {{{ app_name }}} from \"{app_script}?{components[0]}={comps[0]}\""
Right now it only loads the first component as I just want a prototype. The only issue is when I drop this into a template like so:
createpost.html
<script type="module">
{% v_load_app_component "creator" "internalpostform" "[internalpostform]" %}
// OUTPUTS:
// import { creator } from &quot;static/js/internalpostform.js?internalpostform=internalpostform.html&quot;
creator.mount("#app")
</script>
It outputs the relevant import statement as:
import { creator } from &quot;static/js/internalpostform.js?internalpostform=internalpostform.html&quot;
With the double quotes escaped. Even when I tried to apply the safe
filter ({% v_load_app_component "creator" "internalpostform" "[internalpostform]"|safe %}
) it still escaped the output of my custom tag function.
How can I make it to where the output of my custom tag doesn't automatically have symbols converted to html entities?
答案1
得分: 0
我在 Django 的文档中找到了相关信息。safe
过滤器仅用于变量,即 {{ variable|safe }}
,不适用于标签 {% tag "argument"|safe %}
。
要防止 Django 转义标签的输出,只需使用 {% autoescape off %}
{% autoescape off %}
{% v_load_app_component "creator" "internalpostform" "[internalpostform]" %}
{% endautoescape %}
这将实现所需的行为。
英文:
I found it after a little digging in Django's documentation. The safe
filter is only for variables i.e. {{ variable|safe }}
but does not apply to a tag {% tag "argument"|safe %}
.
To prevent Django from escaping the output of a tag you simply use {% autoescape off %}
{% autoescape off %}
{% v_load_app_component "creator" "internalpostform" "[internalpostform]" %}
{% endautoescape %}
This results in the desired behavior.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论