Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » How to get an dictionary item in template [RSS Feed]

#1 Dec. 26, 2010 18:57:00

B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to get an dictionary item in template


I'm sorry for my english, but on other groups nobody does not answers.

In view defined variables:

production = Production.objects.all ()
images = {}
for product in production:
try:
image = product.image_set.get (somefield = somevalue)
images = image
except:
pass

Now the question is: how to access the elements of images in the
template?

{% for product in production %}
{{ product.title }}
{% if images %}
<Img src = "{{ images.<we must substitute product.id>.name }}" alt =
"{{ images.<we must substitute product.id>.title }}"/>
{% endif %}
{% 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 26, 2010 19:35:43

Mikhail K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to get an dictionary item in template


production = Production.objects.all()
for product in production:
try:
product.image = product.image_set.get (somefield = somevalue)
except Image.DoesNotExist:
pass

{% for product in production %}
{{ product.title }}
<Img src = "{{ product.image.image.name }}" alt =
"{{ product.title }}">
{% endfor %}

or even better implement the 'get_image' method for the Product.

But please note that these solutions are executing SQL queries for
each product. It may be better to denormalize the DB and add a FK
field to Product model pointing to the main Image model.


On 26 дек, 23:56, BigBlogMaker <bigblogma...@gmail.com> wrote:
> I'm sorry for my english, but on other groups nobody does not answers.
>
> In view defined variables:
>
> production = Production.objects.all ()
> images = {}
> for product in production:
>     try:
>         image = product.image_set.get (somefield = somevalue)
>         images = image
>     except:
>         pass
>
> Now the question is: how to access the elements of images in the
> template?
>
> {% for product in production %}
> {{ product.title }}
> {% if images %}
> <Img src = "{{ images.<we must substitute product.id>.name }}" alt =
> "{{ images.<we must substitute product.id>.title }}"/>
> {% endif %}
> {% 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 26, 2010 19:43:41

B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to get an dictionary item in template


Each product can have multiple images.
images are divided by type. it facilitates the withdrawal of the
desired image in the right place.

On 27 дек, 01:35, Mikhail Korobov <kmik...@googlemail.com> wrote:
> production = Production.objects.all()
> for product in production:
>     try:
>         product.image = product.image_set.get (somefield = somevalue)
>     except Image.DoesNotExist:
>         pass
>
> {% for product in production %}
>     {{ product.title }}
>     <Img src = "{{ product.image.image.name }}" alt =
> "{{ product.title }}">
> {% endfor %}
>
> or even better implement the 'get_image' method for the Product.
>
> But please note that these solutions are executing SQL queries for
> each product. It may be better to denormalize the DB and add a FK
> field to Product model pointing to the main Image model.
>
> On 26 дек, 23:56, BigBlogMaker <bigblogma...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I'm sorry for my english, but on other groups nobody does not answers.
>
> > In view defined variables:
>
> > production = Production.objects.all ()
> > images = {}
> > for product in production:
> >     try:
> >         image = product.image_set.get (somefield = somevalue)
> >         images = image
> >     except:
> >         pass
>
> > Now the question is: how to access the elements of images in the
> > template?
>
> > {% for product in production %}
> > {{ product.title }}
> > {% if images %}
> > <Img src = "{{ images.<we must substitute product.id>.name }}" alt =
> > "{{ images.<we must substitute product.id>.title }}"/>
> > {% endif %}
> > {% 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » How to get an dictionary item in template [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support