Re: Dictionaries, templates and best practices

2008-06-17 Thread David Zhou

Couldn't you do:

test = []
test.append({'title': 'My First Title', 'post': 'My First Post'});
test.append({'title': 'My Second Title', 'post': 'My Second Post'});

?

On Jun 18, 2008, at 4:11 PM, Juan Hernandez wrote:

> it does not return any values.
>
> I did post.0.title because the dictionary is established like this:
>
> >>> test
> {0: {'post': 'My First Post', 'title': 'My First Title'}, 1:  
> {'post': 'My Second Post', 'title': 'My Second Title'}}
>
> and it goes on and on
>
> If I could iterate over it, like post.0.title, then post.1.title and  
> so forth, it would be perfect
>
> Thanks for your help
>
>
> On Wed, Jun 18, 2008 at 2:42 PM, Joel Bernstein <[EMAIL PROTECTED]>  
> wrote:
>
> I could be completely mistaken, but can't you replace 'post.0.title'
> and 'post.0.post' with 'x.title' and 'x.post', respectively?
>
> On Jun 17, 1:50 pm, "Juan Hernandez" <[EMAIL PROTECTED]> wrote:
> > Hey there
> >
> > I've been practicing for a while with the template system and I  
> have some
> > questions:
> >
> > Lets say that I create this dictionary:
> >
> > test = dict()
> > test[0] = {'title': 'My First Title', 'post': 'My First Post'}
> > test[1] = {'title': 'My Second Title', 'post': 'My Second Post'}
> > etc... til 10 for example
> >
> > If i want to display that information, the best way would be to  
> iterate over
> > each number and show 'title' and 'post'.
> >
> > My question is: How can I actually iterate over a dictionary like  
> this
> > inside a template? If i do this (assuming that the shown  
> dictionary has been
> > assigned to 'post' in render_to_response)
> >
> > {% for x in post%}
> > {{ post.0.title }}^M
> > 
> > 
> >{{ post.0.post }}^M
> >
> > It will only show the first part of the dictionary and 'x' as a  
> number
> > cannot be handled. I haven't found a way to have an increasing  
> number to
> > iterate with... Something to replace the zero that increases until  
> is
> > finished by hte condition
> >
> > What would be the best way to display the whole dictionary in a  
> template??
> >
> > Thank you very much as usual
> > jhv
>
>
>
> >

---
David Zhou
[EMAIL PROTECTED]




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Dictionaries, templates and best practices

2008-06-17 Thread Joel Bernstein

Oops, I totally missed that 'test' was a dictionary. This should work
then:

{% for key, x in posts.items %}
{{ x.title }}


{{ x.post }}
{% endfor %}

Alternately, if your keys are just going to be sequential integers,
you could just put this stuff into a list and use my first suggestion.

On Jun 17, 3:41 pm, "Juan Hernandez" <[EMAIL PROTECTED]> wrote:
> it does not return any values.
>
> I did post.0.title because the dictionary is established like this:
>
> >>> test
>
> {0: {'post': 'My First Post', 'title': 'My First Title'}, 1: {'post': 'My
> Second Post', 'title': 'My Second Title'}}
>
> and it goes on and on
>
> If I could iterate over it, like post.0.title, then post.1.title and so
> forth, it would be perfect
>
> Thanks for your help
>
> On Wed, Jun 18, 2008 at 2:42 PM, Joel Bernstein <[EMAIL PROTECTED]> wrote:
>
> > I could be completely mistaken, but can't you replace 'post.0.title'
> > and 'post.0.post' with 'x.title' and 'x.post', respectively?
>
> > On Jun 17, 1:50 pm, "Juan Hernandez" <[EMAIL PROTECTED]> wrote:
> > > Hey there
>
> > > I've been practicing for a while with the template system and I have some
> > > questions:
>
> > > Lets say that I create this dictionary:
>
> > > test = dict()
> > > test[0] = {'title': 'My First Title', 'post': 'My First Post'}
> > > test[1] = {'title': 'My Second Title', 'post': 'My Second Post'}
> > > etc... til 10 for example
>
> > > If i want to display that information, the best way would be to iterate
> > over
> > > each number and show 'title' and 'post'.
>
> > > My question is: How can I actually iterate over a dictionary like this
> > > inside a template? If i do this (assuming that the shown dictionary has
> > been
> > > assigned to 'post' in render_to_response)
>
> > > {% for x in post%}
> > > {{ post.0.title }}^M
> > > 
> > > 
> > >        {{ post.0.post }}^M
>
> > > It will only show the first part of the dictionary and 'x' as a number
> > > cannot be handled. I haven't found a way to have an increasing number to
> > > iterate with... Something to replace the zero that increases until is
> > > finished by hte condition
>
> > > What would be the best way to display the whole dictionary in a
> > template??
>
> > > Thank you very much as usual
> > > jhv
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Dictionaries, templates and best practices

2008-06-17 Thread Norman Harman

Juan Hernandez wrote:
> it does not return any values.
> 
> I did post.0.title because the dictionary is established like this:
> 
>  >>> test
> {0: {'post': 'My First Post', 'title': 'My First Title'}, 1: {'post': 
> 'My Second Post', 'title': 'My Second Title'}}
> 
> and it goes on and on
> 
> If I could iterate over it, like post.0.title, then post.1.title and so 
> forth, it would be perfect
> 
> Thanks for your help
Don't construct dict like that, make a list instead.
   [ {'post': 'My First Post', 'title': 'My First Title'}, {'post': ... ]


Or, if you don't have control over dict construction, munge it into list.

If order isn't important:
   list_o_dicts = dict_that_should_be_list.values()

If order is important:
   list_o_dicts = [ dict_that_should_be_list[k] for k in 
sorted(dict_that_should_be_list.keys()) ]

Then you can use the template for loop construct normally

{% for dict in list_o_dicts %}
   {{ dict.post }} {{ dict.title }}
{% endfor  %}

-- 
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___
You've got fun!  Check out Austin360.com for all the entertainment
info you need to live it up in the big city!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Dictionaries, templates and best practices

2008-06-17 Thread Juan Hernandez
it does not return any values.

I did post.0.title because the dictionary is established like this:

>>> test
{0: {'post': 'My First Post', 'title': 'My First Title'}, 1: {'post': 'My
Second Post', 'title': 'My Second Title'}}

and it goes on and on

If I could iterate over it, like post.0.title, then post.1.title and so
forth, it would be perfect

Thanks for your help


On Wed, Jun 18, 2008 at 2:42 PM, Joel Bernstein <[EMAIL PROTECTED]> wrote:

>
> I could be completely mistaken, but can't you replace 'post.0.title'
> and 'post.0.post' with 'x.title' and 'x.post', respectively?
>
> On Jun 17, 1:50 pm, "Juan Hernandez" <[EMAIL PROTECTED]> wrote:
> > Hey there
> >
> > I've been practicing for a while with the template system and I have some
> > questions:
> >
> > Lets say that I create this dictionary:
> >
> > test = dict()
> > test[0] = {'title': 'My First Title', 'post': 'My First Post'}
> > test[1] = {'title': 'My Second Title', 'post': 'My Second Post'}
> > etc... til 10 for example
> >
> > If i want to display that information, the best way would be to iterate
> over
> > each number and show 'title' and 'post'.
> >
> > My question is: How can I actually iterate over a dictionary like this
> > inside a template? If i do this (assuming that the shown dictionary has
> been
> > assigned to 'post' in render_to_response)
> >
> > {% for x in post%}
> > {{ post.0.title }}^M
> > 
> > 
> >{{ post.0.post }}^M
> >
> > It will only show the first part of the dictionary and 'x' as a number
> > cannot be handled. I haven't found a way to have an increasing number to
> > iterate with... Something to replace the zero that increases until is
> > finished by hte condition
> >
> > What would be the best way to display the whole dictionary in a
> template??
> >
> > Thank you very much as usual
> > jhv
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Dictionaries, templates and best practices

2008-06-17 Thread Joel Bernstein

I could be completely mistaken, but can't you replace 'post.0.title'
and 'post.0.post' with 'x.title' and 'x.post', respectively?

On Jun 17, 1:50 pm, "Juan Hernandez" <[EMAIL PROTECTED]> wrote:
> Hey there
>
> I've been practicing for a while with the template system and I have some
> questions:
>
> Lets say that I create this dictionary:
>
> test = dict()
> test[0] = {'title': 'My First Title', 'post': 'My First Post'}
> test[1] = {'title': 'My Second Title', 'post': 'My Second Post'}
> etc... til 10 for example
>
> If i want to display that information, the best way would be to iterate over
> each number and show 'title' and 'post'.
>
> My question is: How can I actually iterate over a dictionary like this
> inside a template? If i do this (assuming that the shown dictionary has been
> assigned to 'post' in render_to_response)
>
> {% for x in post%}
> {{ post.0.title }}^M
> 
> 
>{{ post.0.post }}^M
>
> It will only show the first part of the dictionary and 'x' as a number
> cannot be handled. I haven't found a way to have an increasing number to
> iterate with... Something to replace the zero that increases until is
> finished by hte condition
>
> What would be the best way to display the whole dictionary in a template??
>
> Thank you very much as usual
> jhv
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---