Discussion:
merge images into a webpage
het.oosten
2011-07-09 19:52:56 UTC
Permalink
I have two models. One containing images, and one containing the text
of a web page. While the text remains the same, the selection of
images vary, depending on the session ( I use different themes/moods
depending on the season). The selection of images works perfectly, but
now i am stuck merging the images into the web page.

Ideally I would like to use image tags like {{ image1 }} {{ image2 }}
in the web text, and render the result in a web page. I have done
something similar using javascript, but due to seo issues javascript
is not an option for this site.

Can anybody point me in the right direction how to accomplish this?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Shawn Milochik
2011-07-09 19:56:26 UTC
Permalink
Post by het.oosten
I have two models. One containing images, and one containing the text
of a web page. While the text remains the same, the selection of
images vary, depending on the session ( I use different themes/moods
depending on the season). The selection of images works perfectly, but
now i am stuck merging the images into the web page.
Ideally I would like to use image tags like {{ image1 }} {{ image2 }}
in the web text, and render the result in a web page. I have done
something similar using javascript, but due to seo issues javascript
is not an option for this site.
Can anybody point me in the right direction how to accomplish this?
Would using the get_absolute_url() method on your model and then using
that in your <img> tags work?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-09 20:20:08 UTC
Permalink
Post by Shawn Milochik
Would using the get_absolute_url() method on your model and then using
that in your <img> tags work?
thanks for the reply. This works when you have one dictionary and one
template. I want to merge the output of two queries, and render this
in a template.

Query one gives me:
lots of text
{{ image1 }}
even more text
{{ image2 }}

Query two gives me:
<img src="path to my image1">
<img src="path to my image2">

Merging those two would give me:
lots of text
<img src="path to my image1">
even more text
<img src="path to my image2">

I would render the result in a template
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Shawn Milochik
2011-07-09 20:36:33 UTC
Permalink
Make an iterable in your view (list or tuple) containing lists or tuples
of two items -- the text and the image link.

Then iterate through that in your template with a 'for' tag.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-09 21:01:53 UTC
Permalink
Post by Shawn Milochik
Make an iterable in your view (list or tuple) containing lists or tuples
of two items -- the text and the image link.
Then iterate through that in your template with a 'for' tag.
This doesn't work. The webpage text is one object (just one
textfield), and the images are multiple objects.The list of images
would be added before or after the text, instead of replacing
{{ image1 }} with <img src="pathto my images1"> I would get:
lots of text
even more text
<img src="path to my image1">
<img src="path to my image2">

Instead of the desired:
lots of text
<img src="path to my image1">
even more text
<img src="path to my image2">
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Shawn Milochik
2011-07-09 21:14:56 UTC
Permalink
It would work just fine. Something like this:

{% for text, images in my_iterable %}

{{ text }}

{% for image in images %}

<img src="{{image}}" alt="image" />

{% endfor %}


{% endfor %}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 10:41:52 UTC
Permalink
I got is working like this:

In my model:
titel = models.CharField(max_length=200)
alt = models.CharField(max_length=200)
plaatje = models.ImageField(upload_to='plaatjes/')
menu = models.ForeignKey(Menu)
seizoenen = models.ManyToManyField(Seizoenen)
def __unicode__(self):
return '<img src=\"/%s\" alt=\"%s\">' % (self.plaatje,
self.alt)

In my view:
content_tekst = Tekst.objects.filter(menu__slug__iexact=seizoen)[:1]
content_images =
Images.objects.filter(menu__slug__iexact=seizoen)
from django.template import Context, Template
for x in content_text:
t = Template(x.text)
c = Context({"image": content_images})
y = t.render(c)

In the text i store in the database (content_text):
text
{{ image.0| safe}}
more text
{{ image.1|safe}}

Finally I render y in an existing template with a RequestContext
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 10:43:46 UTC
Permalink
Oops i posted too fast....is there a better way to accomplish the
above?
        titel = models.CharField(max_length=200)
        alt = models.CharField(max_length=200)
        plaatje = models.ImageField(upload_to='plaatjes/')
        menu = models.ForeignKey(Menu)
        seizoenen = models.ManyToManyField(Seizoenen)
                return '<img src=\"/%s\" alt=\"%s\">' % (self.plaatje,
self.alt)
content_tekst = Tekst.objects.filter(menu__slug__iexact=seizoen)[:1]
        content_images =
Images.objects.filter(menu__slug__iexact=seizoen)
        from django.template import Context, Template
                t = Template(x.text)
                c = Context({"image": content_images})
                y = t.render(c)
text
{{ image.0| safe}}
more text
{{ image.1|safe}}
Finally I render y in an existing template with a RequestContext
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Shawn Milochik
2011-07-10 15:54:48 UTC
Permalink
I'd rather see the contents of your __unicode__ function as a
get_absolute_url function, with the __unicode__ maybe returning just the
filename or the parent object's title and the filename.

I strongly dislike the embedding of template code in a text field of
your model, because it makes maintenance a lot harder. I don't know what
your code looks like that reads and writes that, so maybe it works for
you. In any case, have a look at adding a method or property to your
model that returns an iterable of image links. I think it'll be a lot
easier to maintain in the long run.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 16:28:46 UTC
Permalink
Adding a method/property to my model is a thing i haven't considered.
You have a good point there. I will do some more reading to find a
solution in this direction. Thanks!

Rob
Post by Shawn Milochik
I'd rather see the contents of your __unicode__ function as a
get_absolute_url function, with the __unicode__ maybe returning just the
filename or the parent object's title and the filename.
I strongly dislike the embedding of template code in a text field of
your model, because it makes maintenance a lot harder. I don't know what
your code looks like that reads and writes that, so maybe it works for
you. In any case, have a look at adding a method or property to your
model that returns an iterable of image links. I think it'll be a lot
easier to maintain in the long run.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Shawn Milochik
2011-07-10 16:31:43 UTC
Permalink
Post by het.oosten
Adding a method/property to my model is a thing i haven't considered.
You have a good point there. I will do some more reading to find a
solution in this direction. Thanks!
Rob
It's easy to forget that Django is just Python. You're the programmer,
so if you want to add a method then go ahead. It's best to understand
Django's design as well as possible before you go adding things because
sometimes things are done for a good reason.

But in the end, you're the developer responsible for delivering a
working product, so do what you have to do.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 17:26:18 UTC
Permalink
Ok perhaps a basic question. I wrote a custom method for my model:
def image_list(self):
return '<img src=\"/media/%s\" alt=\"%s\">' %
(self.image, self.alt)
And i indeed get a nice list of image in the template. I want however
control where the single images show up. With something like {{image.
1}} other text {{image.2}} in the text.

How do i accomplist this?
Post by Shawn Milochik
I'd rather see the contents of your __unicode__ function as a
get_absolute_url function, with the __unicode__ maybe returning just the
filename or the parent object's title and the filename.
I strongly dislike the embedding of template code in a text field of
your model, because it makes maintenance a lot harder. I don't know what
your code looks like that reads and writes that, so maybe it works for
you. In any case, have a look at adding a method or property to your
model that returns an iterable of image links. I think it'll be a lot
easier to maintain in the long run.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Cal Leeming [Simplicity Media Ltd]
2011-07-10 17:49:54 UTC
Permalink
Post by het.oosten
return '<img src=\"/media/%s\" alt=\"%s\">' %
(self.image, self.alt)
You should never really put HTML inside a method unless you *really* have to
(even if it's for performance, unless your site is getting thousands and
thousands of hits a minute, then just use a template)
Post by het.oosten
And i indeed get a nice list of image in the template. I want however
control where the single images show up. With something like {{image.
1}} other text {{image.2}} in the text.
I'm a bit confused as to how you are getting the image list in the first
place, you'll need to paste your code. (use dpaste or pastebin please).
Post by het.oosten
How do i accomplist this?
Post by Shawn Milochik
I'd rather see the contents of your __unicode__ function as a
get_absolute_url function, with the __unicode__ maybe returning just the
filename or the parent object's title and the filename.
I strongly dislike the embedding of template code in a text field of
your model, because it makes maintenance a lot harder. I don't know what
your code looks like that reads and writes that, so maybe it works for
you. In any case, have a look at adding a method or property to your
model that returns an iterable of image links. I think it'll be a lot
easier to maintain in the long run.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 18:04:44 UTC
Permalink
Here is the link to pastebin:
http://pastebin.com/1FuVH7Hu

It is all about getting specific content for a season a user chooses
(it is a site for a campground)

If you need more info, i will be happy to provide.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Shawn Milochik
2011-07-10 19:20:11 UTC
Permalink
You can easily do this in your template with an index. Assuming you'll
have the variable image_list in your context which is a list, you can do
this:

{{ image_list.0 }} and {{ image_list.1 }}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 19:28:02 UTC
Permalink
Post by Shawn Milochik
have the variable image_list in your context which is a list, you can do
{{ image_list.0 }} and {{ image_list.1 }}
This is what i tried, but the only way to show the images
is(plaatjes_lijst=image_list):
{% for x in plaatje %}
{{x.plaatjes_lijst|safe}}
{% endfor %}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 19:33:42 UTC
Permalink
One addition to the above, when i do:
def __unicode__(self):
return '<img src=\"/media/%s\" alt=\"%s\">' %
(self.plaatje, self.alt)

I can call the single images in the template with {{plaatje.1}} etc
I found out in this thread that it is not a good idea to "abuse"
unicode for this
Post by het.oosten
Post by Shawn Milochik
have the variable image_list in your context which is a list, you can do
{{ image_list.0 }} and {{ image_list.1 }}
This is what i tried, but the only way to show the images
{% for x in plaatje %}
{{x.plaatjes_lijst|safe}}
{% endfor %}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Cal Leeming [Simplicity Media Ltd]
2011-07-10 19:40:21 UTC
Permalink
Nice, not often I come across code written in nederlands ;p

Is this what you are looking for by any chance??

{{plaatje.1.plaatjes_lijst}}
Post by het.oosten
return '<img src=\"/media/%s\" alt=\"%s\">' %
(self.plaatje, self.alt)
I can call the single images in the template with {{plaatje.1}} etc
I found out in this thread that it is not a good idea to "abuse"
unicode for this
Post by het.oosten
Post by Shawn Milochik
have the variable image_list in your context which is a list, you can
do
Post by het.oosten
Post by Shawn Milochik
{{ image_list.0 }} and {{ image_list.1 }}
This is what i tried, but the only way to show the images
{% for x in plaatje %}
{{x.plaatjes_lijst|safe}}
{% endfor %}
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 19:58:21 UTC
Permalink
Yessss, I think this was the only option i didnt try :-)

Thanks Cal and Shawn!

In a previous i translated the Dutch into English, and mixed things
up, making everything more confusing.

Rob

On Jul 10, 9:40 pm, "Cal Leeming [Simplicity Media Ltd]"
Post by Cal Leeming [Simplicity Media Ltd]
Nice, not often I come across code written in nederlands ;p
Is this what you are looking for by any chance??
{{plaatje.1.plaatjes_lijst}}
                return '<img src=\"/media/%s\" alt=\"%s\">' %
(self.plaatje, self.alt)
I can call the single images in the template with {{plaatje.1}} etc
I found out in this thread that it is not a good idea to "abuse"
unicode for this
Post by het.oosten
Post by Shawn Milochik
have the variable image_list in your context which is a list, you can
do
Post by het.oosten
Post by Shawn Milochik
{{ image_list.0 }} and {{ image_list.1 }}
This is what i tried, but the only way to show the images
{% for x in plaatje %}
{{x.plaatjes_lijst|safe}}
{% endfor %}
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Cal Leeming [Simplicity Media Ltd]
2011-07-10 20:10:03 UTC
Permalink
Post by het.oosten
Yessss, I think this was the only option i didnt try :-)
Thanks Cal and Shawn!
In a previous i translated the Dutch into English, and mixed things
up, making everything more confusing.
Just so happens I'm learning Dutch at the moment, and was able to read a lot
of the code ;p
Post by het.oosten
Rob
On Jul 10, 9:40 pm, "Cal Leeming [Simplicity Media Ltd]"
Post by Cal Leeming [Simplicity Media Ltd]
Nice, not often I come across code written in nederlands ;p
Is this what you are looking for by any chance??
{{plaatje.1.plaatjes_lijst}}
Post by het.oosten
return '<img src=\"/media/%s\" alt=\"%s\">' %
(self.plaatje, self.alt)
I can call the single images in the template with {{plaatje.1}} etc
I found out in this thread that it is not a good idea to "abuse"
unicode for this
Post by het.oosten
Post by Shawn Milochik
have the variable image_list in your context which is a list, you
can
Post by Cal Leeming [Simplicity Media Ltd]
Post by het.oosten
do
Post by het.oosten
Post by Shawn Milochik
{{ image_list.0 }} and {{ image_list.1 }}
This is what i tried, but the only way to show the images
{% for x in plaatje %}
{{x.plaatjes_lijst|safe}}
{% endfor %}
--
You received this message because you are subscribed to the Google
Groups
Post by Cal Leeming [Simplicity Media Ltd]
Post by het.oosten
"Django users" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
het.oosten
2011-07-10 20:17:41 UTC
Permalink
Post by Cal Leeming [Simplicity Media Ltd]
Just so happens I'm learning Dutch at the moment, and was able to read a lot
of the code ;p
I guess that was my luck :-)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Continue reading on narkive:
Loading...