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>