DRYer way to do this

8 views
Skip to first unread message

Thomas

unread,
Mar 31, 2012, 8:51:28 AM3/31/12
to satchm...@googlegroups.com
I am convinced that there must be a way to this in a loop in the template:

    <table class="indented">
        <tr><td>Contact :</td><td>{{ product.extendedproductfields.contact }}</td></tr>
        <tr><td>Email :  </td><td>{{ product.extendedproductfields.email }}</td></tr>
        <tr><td>Phone :  </td><td>{{ product.extendedproductfields.phone }}</td></tr>
    </table>

How can I print the 'Email', 'Phone', .. fields automatically in the table above rather than hard-code them?

Many thanks,
Thomas

Dave Brown

unread,
Mar 31, 2012, 9:13:56 AM3/31/12
to satchm...@googlegroups.com
Well, if contact, email, and phone were some sort of relationship instead of just a charfield/email field, you could loop through them (lets say they were a many2many to 'field'), and have a label (maybe help_text) associated with each field... but thats making it more complicated then it needs to be... it isn't breaking the DRY principle really.. these are each a unique field that need to be handled individually.. your not hardcoding them.. that's like saying you hardcoded the code of your application because code was written. 

Dave

--
You received this message because you are subscribed to the Google Groups "Satchmo users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/satchmo-users/-/TF5dAJEINYIJ.
To post to this group, send email to satchm...@googlegroups.com.
To unsubscribe from this group, send email to satchmo-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/satchmo-users?hl=en.



--
Dave Brown
CEO/Founder
Rampframe.com - Action Sports Network

Thomas

unread,
Mar 31, 2012, 10:15:28 AM3/31/12
to satchm...@googlegroups.com
Sorry I wasn't being too clear. I would like to use the name of the field (just with the first letter capitalized) as a label. e.g. the field named 'email' would be labeled 'Email'. I just don't know how to do that.


On Saturday, March 31, 2012 3:13:56 PM UTC+2, BoulderDave wrote:
Well, if contact, email, and phone were some sort of relationship instead of just a charfield/email field, you could loop through them (lets say they were a many2many to 'field'), and have a label (maybe help_text) associated with each field... but thats making it more complicated then it needs to be... it isn't breaking the DRY principle really.. these are each a unique field that need to be handled individually.. your not hardcoding them.. that's like saying you hardcoded the code of your application because code was written. 

Dave

On Sat, Mar 31, 2012 at 8:51 AM, Thomas <tea.a...@gmail.com> wrote:
I am convinced that there must be a way to this in a loop in the template:

    <table class="indented">
        <tr><td>Contact :</td><td>{{ product.extendedproductfields.contact }}</td></tr>
        <tr><td>Email :  </td><td>{{ product.extendedproductfields.email }}</td></tr>
        <tr><td>Phone :  </td><td>{{ product.extendedproductfields.phone }}</td></tr>
    </table>

How can I print the 'Email', 'Phone', .. fields automatically in the table above rather than hard-code them?

Many thanks,
Thomas

--
You received this message because you are subscribed to the Google Groups "Satchmo users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/satchmo-users/-/TF5dAJEINYIJ.
To post to this group, send email to satchm...@googlegroups.com.
To unsubscribe from this group, send email to satchmo-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/satchmo-users?hl=en.

Dave Brown

unread,
Mar 31, 2012, 11:07:24 AM3/31/12
to satchm...@googlegroups.com
Try looking here, some good information I think



To view this discussion on the web visit https://groups.google.com/d/msg/satchmo-users/-/cHv4t0ka3LgJ.

To post to this group, send email to satchm...@googlegroups.com.
To unsubscribe from this group, send email to satchmo-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/satchmo-users?hl=en.

hynekcer

unread,
Mar 31, 2012, 11:59:24 AM3/31/12
to Satchmo users
First answer: {{ something|capfirst }} RTFM

The second is not only for template but easy.
functional example concentrated on 4 lines

./manage.py shell
from django.template import Template, Context
p = Product.objects.all()[0]
t = Template('{% for name,value in prod_dict %}{{ name|capfirst }}:
{{ value }}\n{% endfor %}')
print t.render(Context({'prod_dict': sorted(p.__dict__.items())}))


--Hy

On 31 bře, 16:15, Thomas <tea.andr...@gmail.com> wrote:
> Sorry I wasn't being too clear. I would like to use the name of the field
> (just with the first letter capitalized) as a label. e.g. the field named
> 'email' would be labeled 'Email'. I just don't know how to do that.
>
>
>
>
>
>
>
> On Saturday, March 31, 2012 3:13:56 PM UTC+2, BoulderDave wrote:
>
> > Well, if contact, email, and phone were some sort of relationship instead
> > of just a charfield/email field, you could loop through them (lets say they
> > were a many2many to 'field'), and have a label (maybe help_text) associated
> > with each field... but thats making it more complicated then it needs to
> > be... it isn't breaking the DRY principle really.. these are each a unique
> > field that need to be handled individually.. your not hardcoding them..
> > that's like saying you hardcoded the code of your application because code
> > was written.
>
> > Dave
>
> > On Sat, Mar 31, 2012 at 8:51 AM, Thomas <tea.andr...@gmail.com> wrote:
>
> >> I am convinced that there must be a way to this in a loop in the template:
>
> >>     <table class="indented">
> >>         <tr><td>Contact :</td><td>{{
> >> product.extendedproductfields.contact }}</td></tr>
> >>         <tr><td>Email :  </td><td>{{ product.extendedproductfields.email
> >> }}</td></tr>
> >>         <tr><td>Phone :  </td><td>{{ product.extendedproductfields.phone
> >> }}</td></tr>
> >>     </table>
>
> >> How can I print the 'Email', 'Phone', .. fields automatically in the
> >> table above rather than hard-code them?
>
> >> Many thanks,
> >> Thomas
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Satchmo users" group.
> >> To view this discussion on the web visit
> >>https://groups.google.com/d/msg/satchmo-users/-/TF5dAJEINYIJ.
> >> To post to this group, send email to satchm...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> satchmo-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages