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

閲覧: 25 回
最初の未読メッセージにスキップ

Dennis Marwood

未読、
2015/08/30 15:43:462015/08/30
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

未読、
2015/08/30 17:03:182015/08/30
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

未読、
2015/08/30 17:11:452015/08/30
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

未読、
2015/08/30 17:37:492015/08/30
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

未読、
2015/08/30 17:48:472015/08/30
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

未読、
2015/08/30 18:00:152015/08/30
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.
全員に返信
投稿者に返信
転送
新着メール 0 件