Wednesday, February 26, 2014

Re: Accessing a python function from a blog post stored in the db?

On Wed, Feb 26, 2014 at 5:29 AM, Dennis Marwood <dennismarwood@gmail.com> wrote:
> Thanks. I believe this is what I am looking for. However I am still running
> into an issue w/ the fact that I am pulling my blog entries from the db.
> This is causing the template to interpret the {% image_slider %} as a
> string.
>
> From view.py:
> def blog(request):
> entries =
> Project.objects.filter(destination="blog").order_by('-creation_date')
> return render(request, 'content.html', {"entries": entries,
> "page_title": "Blog",
> "description": "Blog"})
>
> And in content.html:
> {% for each in entries %}
> <p>{{ each.entry }}</p>
> {% endfor %}
>
> And a blog entry:
> Hello! Check out this collection of images. {% image_slider %} And this
> collection! {% image_slider %}
>
>
> I don't want to hard code the inclusion tag in the content.html. Instead I
> want to call it from the blog post. Is this possible?

Yes. You will need to write a custom template tag to parse the
contents of the object as a template, render it within the current
context and return the generated data.

Writing a custom template tag:

https://docs.djangoproject.com/en/1.6/howto/custom-template-tags/#writing-custom-template-tags

Rendering templates to string:

https://docs.djangoproject.com/en/1.6/ref/templates/api/#rendering-a-context

Your content.html would then look something like this, assuming you
called the custom tag 'render':

{% for each in entries %}
<p>{% render each.entry %}</p>
{% endfor %}

Beware the massive security holes if you allow users to generate
content that your site then renders as a template.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1%2BkDdaL5UqY_npcVfnPsZNisffOhzme-C768tC%3DnAycMA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment