Is there a way to make the template process {{ MEDIA_URL }} if it is stored in a model field.

25 views
Skip to first unread message

Dennis Marwood

unread,
Aug 30, 2015, 3:43:46 PM8/30/15
to Django users
Hello,

I have a seemingly straight forward and likely common issue. But there is no obvious way that I am seeing to make it work.

I have a Blog model with a text field called entry. I want to put something like

<img src="{{ MEDIA_URL }}/images/my_image.jpg">

into the blog post. So Blog.entry would contain it. Then I use the template to display my blog post like

{% for post in posts %} <p>{{ post.entry }}</p> {% endfor %}

But the template does not process {{ MEDIA_URL }}. It just sees it as a string and prints it exactly as passed in.

How do I get the template to treat {{ }} in my entry as a variable? How can I get that value substituted? I have used a custom template filter in the past. But this has got to be a very common use case. It seems like there would be some built in django way to get this to work?

Thanks,
Dennis

monoBOT

unread,
Aug 30, 2015, 5:03:18 PM8/30/15
to django...@googlegroups.com
You can import your any part of your settings into the model.py file and then insert it

like this:

from django.conf import MEDIA_URL


--
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...@googlegroups.com.
To post to this group, send email to django...@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/60b7bdc9-f396-4ffd-b4ff-10a7dd5931c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/

Dennis Marwood

unread,
Aug 30, 2015, 5:11:45 PM8/30/15
to Django users
So you are saying I should alter the models save method to do a search and replace on the entry field?

Christophe Pettus

unread,
Aug 30, 2015, 5:37:49 PM8/30/15
to django...@googlegroups.com

On Aug 30, 2015, at 12:43 PM, Dennis Marwood <dennis...@gmail.com> wrote:

> How do I get the template to treat {{ }} in my entry as a variable?

The Django template language doesn't iterate over the results of expansion; once a substitution is done, it moves on. So, template language constructs that "appear" in the result due to variable expansion

You could do this by building the template up programmatically, and then passing the output of that process to the appropriate render method. You could also (probably better) write a custom template tag that does the right thing.

--
-- Christophe Pettus
x...@thebuild.com

Christophe Pettus

unread,
Aug 30, 2015, 5:48:47 PM8/30/15
to django...@googlegroups.com

On Aug 30, 2015, at 2:37 PM, Christophe Pettus <x...@thebuild.com> wrote:

> On Aug 30, 2015, at 12:43 PM, Dennis Marwood <dennis...@gmail.com> wrote:
>
>> How do I get the template to treat {{ }} in my entry as a variable?
>
> The Django template language doesn't iterate over the results of expansion; once a substitution is done, it moves on. So, template language constructs that "appear" in the result due to variable expansion.

As a note, this is a feature, not a bug: If template expansion iterated like that, it would be a serious security hole, since someone could drop template items ( {{ entry.delete }}, anyone?) into user-edited content.

Dennis Marwood

unread,
Aug 30, 2015, 6:00:15 PM8/30/15
to Django users
Ok, thanks. It seems like such a common use case that I wanted to be sure there was not an existing template tag for it.
Reply all
Reply to author
Forward
0 new messages