从Django中的数据库呈现自定义模板标签 [英] Rendering custom template tags from database in Django
问题描述
这是我的观点:
from django.shortcuts import render_to_response
from myproject.apps.blog.models import Post
def blog_detail_view(request,year,month,day,slug):
selected_post = Post.objects.get(status = 1,pub_date__year = year,pub_date__month = time.strptime(month,%b)[1],pub_date__day = day,slug = slug)
return render_to_response blog / detail.html',locals())
我的模板的相关部分: p>
...
{%load my_custom_tag%}
< div class =entry >
{{selected_post.body | safe}}
< / div>
...
以下是保存到 Post.body
:
我的第一段。
{%my_custom_tag var1 var2%}
我的第二段。
目前,这将呈现为:
< p>我的第一个段落< / p>
< p> {%my_custom_tag var1 var2%}< / p>
< p>我的第二段。< / p>
当我想要这样的东西:
< p>我的第一个段落< / p>
< p>< a href =var1>< img src =var2>< / a>< / p>
< p>我的第二段。< / p>
事实证明, render_as_template
标签发现这里工作,我只是误解了它。初学者的错误在我自己无法接受的代码段的文档中没有指定两件事。
所以,你把您的模板中的render_as_template
标签,如下所示:
{%load render_as_template%}
{ %render_as_template myobject.attribute%}
因此,允许 myobject中的任何模板标签。属性
。这是我遇到麻烦的地方:
首先,我将我的帖子正文传递给 render_as_template
为 selected_post.body | truncatewords_html:100| safe
或作为 selected_post.body | safe
。模板标签无法处理属性上的过滤器,似乎将其解释为对象名称的一部分。由于此对象不存在,因此不会呈现任何内容。我在模板中需要的是:
{%autoescape off%}
{%filter truncatewords_html:100 $}
{%render_as_template selected_post.body%}
{%endfilter%}
{%endautoescape%}
第二,如果我的帖子的内容是:
我的第一段。
{%my_custom_tag var1 var2%}
我的第二段。
然后为了 my_custom_tag
被解释,我需要添加到我的帖子中:
{%load my_custom_tag%}
现在它的效果很好!
I am pretty new to Django and am trying to build a blog app for my website. I have a model I have created to store blog posts that includes a text field for the post body, created and converted to HTML using TinyMCE (via Grappelli). I would like embed custom template tags within this post body that are saved in the database as the template tag, but then rendered as HTML when requested on my site. So far, I am not having any luck getting the tags to render properly. How can I get Django to correctly interpret and render the template tags within my post? The custom tag works fine when loaded and implemented directly in a template, it is just this indirect loading that gives me trouble. I tried this snippet here (Edit: It works, I just did it wrong!), but it did not work correctly.
Here is my view:
from django.shortcuts import render_to_response
from myproject.apps.blog.models import Post
def blog_detail_view(request, year, month, day, slug):
selected_post = Post.objects.get(status=1, pub_date__year = year, pub_date__month = time.strptime(month, "%b")[1], pub_date__day = day, slug = slug)
return render_to_response('blog/detail.html', locals())
And the relevant portion of my template:
...
{% load my_custom_tag %}
<div class="entry">
{{ selected_post.body|safe }}
</div>
...
Here is an example of the text saved to Post.body
:
My first paragraph.
{% my_custom_tag var1 var2 %}
My second paragraph.
Currently, this will render as :
<p>My first paragraph.</p>
<p>{% my_custom_tag var1 var2 %}</p>
<p>My second paragraph.</p>
When I want something like this:
<p>My first paragraph.</p>
<p><a href="var1"><img src="var2"></a></p>
<p>My second paragraph.</p>
It turns out that the render_as_template
tag found here DOES work, I just misunderstood it. Beginner's mistake. There are two things that were not specified in the documentation for the snippet that I failed to pick up on my own.
So, you put the render_as_template
tag in your template like this:
{% load render_as_template %}
{% render_as_template myobject.attribute %}
Thus allowing any template tags within myobject.attribute
to be rendered. Here is where I ran into trouble:
First, I was passing my post body to render_as_template
as selected_post.body|truncatewords_html:"100"|safe
or as selected_post.body|safe
. The template tag is unable to process the filters on the attribute and seems to interpret them as part of the object's name. Since this object does not exist, nothing is rendered. What I need in my template is:
{% autoescape off %}
{% filter truncatewords_html:"100" %}
{% render_as_template selected_post.body %}
{% endfilter %}
{% endautoescape %}
Second, if the content of my post is:
My first paragraph.
{% my_custom_tag var1 var2 %}
My second paragraph.
Then in order for my_custom_tag
to be interpreted, I need to add to my post:
{% load my_custom_tag %}
Now it works perfectly!
这篇关于从Django中的数据库呈现自定义模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!