Re: select_related() and Template Logic

2008-01-29 Thread theZero

> I haven't done exactly this, but I've done something similar, and can
> maybe help with some general principles.


Hi Eric. Thanks for the help and advice!


> It's not clear exactly what's going into these pods, but it's usually
> a bad idea to have things like 'style' or 'placement' in the database
> tables: that's presentational logic, not data. I would suggest either
> {% include %}-ing smaller template snippets, or using custom template
> tags. If the snippet/tag can get all its information from the existing
> context, use an include; if it needs its own database queries/extended
> python, use a tag.

Honestly it's not completely clear to me, either. That could be part
of the problem :)

I was trying to make a relatively simple CMS to get to know django
better. I'm loosely recreating a "website builder" app that would
allow non-technical users to edit and update their websites. The
"pods" could be thought of as content blocks or banner ads or
something similar. Incidentally, I don't call them "pods" in the
application, but for the sake of clarity, I wanted a name that didn't
include "block" or "node." That's the best I could do on short
notice.

As for presentation vs. data, I understand your point.


> Still kind of shooting in the dark: information such as how the pods
> are to be displayed should probably come in through the view. Maybe
> via a GET query, maybe via saved user settings. You could create a
> dictionary in the view, where say the keys were snippet/tag names, and
> the values a list of style/presentation parameters that were passed to
> each appropriate snippet. Something like that. Then your template is
> simply a matter of checking if the dict has certain keys, and then
> using the dict values to populate CSS style attributes, or to set
> variables telling a template tag which queries to perform (actually
> that last might not work, can't remember...).

As you can probably tell, I was trying to get by with the least work
possible and that usually spells disaster. I figured I could call
everything with Page.objects.select_related(), and then have the
template sort out the details.

Interesting that you bring up the custom dict - that's what they seem
to do here:
http://www.carthage.edu/webdev/?p=15


> As always, B-List has the 
> goods:http://www.b-list.org/weblog/2006/jun/07/django-tips-write-better-tem...
>
> Hope something in there helps,

That's what I started to read before I made my first post. Guess I
should review it :)

Well you got me thinking a little harder! Thanks again!

--~--~-~--~~~---~--~~
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: select_related() and Template Logic

2008-01-29 Thread Eric Abrahamsen

> Second, it is obviously and woefully inefficient to do this for
> several blocks on the page. I may have 5 or more, and I may want to
> add some in the future. Is this the place for a custom templatetag?
> Has someone already put something like this together?

I haven't done exactly this, but I've done something similar, and can
maybe help with some general principles.

It's not clear exactly what's going into these pods, but it's usually
a bad idea to have things like 'style' or 'placement' in the database
tables: that's presentational logic, not data. I would suggest either
{% include %}-ing smaller template snippets, or using custom template
tags. If the snippet/tag can get all its information from the existing
context, use an include; if it needs its own database queries/extended
python, use a tag.

Still kind of shooting in the dark: information such as how the pods
are to be displayed should probably come in through the view. Maybe
via a GET query, maybe via saved user settings. You could create a
dictionary in the view, where say the keys were snippet/tag names, and
the values a list of style/presentation parameters that were passed to
each appropriate snippet. Something like that. Then your template is
simply a matter of checking if the dict has certain keys, and then
using the dict values to populate CSS style attributes, or to set
variables telling a template tag which queries to perform (actually
that last might not work, can't remember...).

As always, B-List has the goods:
http://www.b-list.org/weblog/2006/jun/07/django-tips-write-better-template-tags/

Hope something in there helps,
Eric
--~--~-~--~~~---~--~~
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: select_related() and Template Logic

2008-01-29 Thread Eduardo - IdNotFound

Hello,

On Jan 28, 2008 3:09 PM, theZero <[EMAIL PROTECTED]> wrote:
>
[introduction]
>
> Pages are called using select_related(), which retrieves the
> associated Pods. So in the template, I have several areas like this:
>
> {% block leftcolumn %}
> 
> {% for pod in page.pods.all %}
> {% ifequal pod.placement "left" %}
> 
> {{ pod.Title }}
> {{ pod.Content|escape|linebreaks }}
> 
> {% else %}
> {% endifequal %}
> {% endfor %}
> 
> {% endblock %}
>
> It works, but I definitely know it's not the best way to do it. I have
> two primary problems with it.
>
> First, if there are no items in that block, then I don't want to show
> that  on the page at all. How can I choose to
> display the contents of a block depending on if a pod exists that
> meets the select criteria? I know I'm missing something simple, but I
> can't put my finger on it.

If I am not mistaken, {% if page.pods.all %} should evaluate to False
if there are no pods, so you can enclose your  with that and it
should work as you'd like.

> Second, it is obviously and woefully inefficient to do this for
> several blocks on the page. I may have 5 or more, and I may want to
> add some in the future. Is this the place for a custom templatetag?
> Has someone already put something like this together?
>
> If I'm completely off-base and thinking about this wrong, let me know.
> I'm still learning!

I have no experience with this second issue. Sorry!


Hope it helps,
Eduardo.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



select_related() and Template Logic

2008-01-28 Thread theZero

Hi all. I'm pretty new to django, but I'm catching on slowly but
surely. So please bear with me :)

For one site I'm working/learning on, I'm trying to build in "Pods" of
static content that can be added as necessary to any page (or later,
series of pages). The individual pods can be moved into different
content areas of the page, depending on the content manager's wishes.
Here's a simplified sample of the models:

class Page(models.Model): # mostly borrowed from flatpages
title = models.CharField(_('title'), max_length=200)
content = models.TextField(_('content'))
pods = models.ManyToManyField('pod', blank=True)
...

class Pod(models.Model):
name = models.CharField(max_length=100)
content = models.TextField(blank=False)
order = models.SmallIntegerField(choices=POD_ORDER_CHOICES)
style = models.CharField(max_length=20,choices=POD_STYLE_CHOICES)
placement =
models.CharField(max_length=20,choices=POD_PLACEMENT_CHOICES)
...

Pages are called using select_related(), which retrieves the
associated Pods. So in the template, I have several areas like this:

{% block leftcolumn %}

{% for pod in page.pods.all %}
{% ifequal pod.placement "left" %}

{{ pod.Title }}
{{ pod.Content|escape|linebreaks }}

{% else %}
{% endifequal %}
{% endfor %}

{% endblock %}

It works, but I definitely know it's not the best way to do it. I have
two primary problems with it.

First, if there are no items in that block, then I don't want to show
that  on the page at all. How can I choose to
display the contents of a block depending on if a pod exists that
meets the select criteria? I know I'm missing something simple, but I
can't put my finger on it.

Second, it is obviously and woefully inefficient to do this for
several blocks on the page. I may have 5 or more, and I may want to
add some in the future. Is this the place for a custom templatetag?
Has someone already put something like this together?

If I'm completely off-base and thinking about this wrong, let me know.
I'm still learning!
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---