0

Sorry if my question sounds stupid as i am new to Django.

I have a view in Django like this :

def myview(request):

list = Ref.objects.all()

return render_to_response('annonces.html', {'list': list} ,RequestContext(request)) 

Ref is a small sqlite database where i store things scraped from the web.

In my django template, i want to loop on a field of my list object (hurl) which contains a list of links (images) in order to show it on my web page. I've tried this :

   {% for value in list %}
       {% for link in value.hurl %}
           <li><img src="{{ link }}"></li>
       {% endfor %}
   {% endfor %}

Unfortunately, it doesn't work. The object list convert everything to string so the loop in my template create a line for each character in the field hurl.

Would you know how i can get my object to render hurl as a list instead of a string ?

Here is an example of value.hurl :

u"['http://3.visuels.poliris.com/thumbnails/3/3/6/5/3365ddc7-3f27.jpg', 'http://1.visuels.poliris.com/thumbnails/1/2/b/6/12b6c286-372f.jpg', 'http://b.visuels.poliris.com/thumbnails/b/4/f/3/b4f32a1c-3159.jpg']"

Here is the model. Not sure if URLField is correct for hurl.

class Ref(models.Model):

ref = models.IntegerField(max_length=10, primary_key='True')
title = models.CharField(max_length=500)
hurl = models.URLField()
url = models.URLField()
prix = models.CharField(max_length=500)

Thanks Gilles

5
  • Can you show us what an example value for the hurl field? Commented Sep 11, 2013 at 21:15
  • I have just added one example :) Commented Sep 11, 2013 at 21:19
  • Can you give us Ref model definition? Commented Sep 11, 2013 at 21:21
  • That's a string, not a list. So when you iterate over it, you get the what you are seeing now. Commented Sep 11, 2013 at 21:21
  • yes that's why i'm asking how i can get a list and not a string. Commented Sep 11, 2013 at 21:28

1 Answer 1

1

You should change your object model and store the multiple values for hurl as another object.

That object should have a ForeignKey to your Ref object:

class Ref(models.Model):
    # snip

class Link(models.Model):
    url = models.CharField(...)
    ref = models.ForeignKey("Ref")

Then, in your template, do:

{% for value in list %}
    {% for link in value.link_set %}
        <li><img src="{{ link }}"></li>
    {% endfor %}
{% endfor %}

For better performance, in your view, do:

list = Ref.objects.prefetch_related("link_set").all()
Sign up to request clarification or add additional context in comments.

2 Comments

Ok Thank you, i'll try and let you know! I will amend my scrappy code and export images in the Link table. It looks like i will have one line per image in the link table then ?
It works ! I've slightly changed your list though. I have : {% for link in value.pic_set.all %} <li><img src="{{ link.pic }}"></li>. Otherwise, the template fails. Thank you very much for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.