|
List Info
Thread: Templates and Views - what counts as "logic"?
|
|
| Templates and Views - what counts as
"logic"? |

|
2006-10-29 02:53:42 |
Hi all,
I'm creating a blog as a first Django project, since that
seems like a
good way to get my feet wet. I've run into a problem
displaying a list
of post excerpts with "read more" links. Here's
my code for the
excerpts:
<ul>
{% for object in object_list %}
<h2><a
href="{{object.id}}/">{{ object.title
}}</a></h2>
<p>{% filter truncatewords:"75" %}{{
object.body }}{% endfilter
%}</p>
{% endfor %}
</ul>
I'd like to display the "read more" link only if
the post is longer
than 75 words, but I can't seem to figure out how to get
that
conditional test into the template.
Does that mean that this is really view logic? It seems a
shame to
have to write a custom view function just to do something
that's as
simple and as display-related as that. Is there any way to
do this in
the template?
Thanks for your help!
--~--~---------~--~----~------------~-------~--~----~
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
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Templates and Views - what counts as
"logic"? |

|
2006-10-29 14:25:12 |
On 10/29/06, ringemup <ringemup gmail.com> wrote:
>
> Does that mean that this is really view logic? It
seems a shame to
> have to write a custom view function just to do
something that's as
> simple and as display-related as that. Is there any
way to do this in
> the template?
You could split this into view logic; however, a better
options is
probably to add a method to your model. Django templates can
call any
method on an object that takes no arguments, so a simple
'is_long'
method on the object could be used to determine if any given
object
has more that 75 words.
For example:
class Article(Model):
... (fields) ...
def is_long(self):
return ...(logic for a long article)...
Then in your template, if object_list is a list of articles:
{% for object in object_list %}
{{ object.body }}
{% if object.is_long %}
... link to 'read more'
{% endif %}
{% endfor %}
Yours,
Russ Magee %-)
--~--~---------~--~----~------------~-------~--~----~
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
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Templates and Views - what counts as
"logic"? |

|
2006-10-29 14:25:12 |
On 10/29/06, ringemup <ringemup gmail.com> wrote:
>
> Does that mean that this is really view logic? It
seems a shame to
> have to write a custom view function just to do
something that's as
> simple and as display-related as that. Is there any
way to do this in
> the template?
You could split this into view logic; however, a better
options is
probably to add a method to your model. Django templates can
call any
method on an object that takes no arguments, so a simple
'is_long'
method on the object could be used to determine if any given
object
has more that 75 words.
For example:
class Article(Model):
... (fields) ...
def is_long(self):
return ...(logic for a long article)...
Then in your template, if object_list is a list of articles:
{% for object in object_list %}
{{ object.body }}
{% if object.is_long %}
... link to 'read more'
{% endif %}
{% endfor %}
Yours,
Russ Magee %-)
--~--~---------~--~----~------------~-------~--~----~
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
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Templates and Views - what counts as
"logic"? |

|
2006-10-29 16:36:25 |
That definitely looks feasible - thanks!
--~--~---------~--~----~------------~-------~--~----~
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
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Templates and Views - what counts as
"logic"? |

|
2006-10-29 16:36:25 |
That definitely looks feasible - thanks!
--~--~---------~--~----~------------~-------~--~----~
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
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Templates and Views - what counts as
"logic"? |

|
2006-10-31 06:42:04 |
I had this proble too. Heres how i solved it.
First i had a model method:
def get_wordcount(self):
return len(self.content.split())
(content is a TextField)
Then in my template i used it like this:
{{ object.content|truncatewords:60 }}
{% load base_utils %}
{% ifgreaterthan object.get_wordcount 60 %}
<a href="/news/{{ object.id
}}">read more</a></p>
{% endifgreaterthan %}
You'll also need to get the ifgreaterthan custom template
tag from
here:
https://svn.nrcfosshelpline.i
n/public/helpline/trunk/web/templatetags/base_utils.py
(you'll need to edit it a bit)
Hope it helps
Sam Morrison
--~--~---------~--~----~------------~-------~--~----~
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
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Templates and Views - what counts as
"logic"? |

|
2006-10-31 06:42:04 |
I had this proble too. Heres how i solved it.
First i had a model method:
def get_wordcount(self):
return len(self.content.split())
(content is a TextField)
Then in my template i used it like this:
{{ object.content|truncatewords:60 }}
{% load base_utils %}
{% ifgreaterthan object.get_wordcount 60 %}
<a href="/news/{{ object.id
}}">read more</a></p>
{% endifgreaterthan %}
You'll also need to get the ifgreaterthan custom template
tag from
here:
https://svn.nrcfosshelpline.i
n/public/helpline/trunk/web/templatetags/base_utils.py
(you'll need to edit it a bit)
Hope it helps
Sam Morrison
--~--~---------~--~----~------------~-------~--~----~
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
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
[1-7]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|