ok, now it looks like

from django import template
register = template.Library()

@register.filter("has_saved_link")
def has_saved_link(user, object):
    return user.savelink_set.objects.filter(link=object).count()

and then in the template it's like

{% if user.is_authenticated and not user|has_saved_link:object %}

The error i get now is  : Caught an exception while rendering:
'RelatedManager' object has no attribute 'objects'

Am i forgetting to include something else ?

Thanks again....

On Oct 14, 2:55 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Oct 14, 1:36 pm, grimmus <graham.col...@gmail.com> wrote:
>
>
>
> > Thanks for your reply. Here's what i did so far
>
> > i created a file called has_saved_link.py in my app's templatetags
> > folder:
>
> > from django.template import Library, Node
> > from django.db.models import get_model
>
> > register = Library()
>
> > def has_saved_link(user, object):
> >     return user.savedlink_set.objects.filter(link=object).count()
>
> > has_saved_link = register.tag(has_saved_link)
>
> > Then, in my template i load the tag
>
> > {% load has_saved_link %}
>
> > But if i try and use your syntax below i get errors
>
> > {% if user.is_authenticated and not user|has_saved_link:object %} -
> > Error - Invalid filter: 'has_saved_link'
> > {% if user.is_authenticated and not user.has_saved_link:object %} -
> > Error - Could not parse the remainder: ':object' from
> > 'user.has_saved_link:object'
>
> > I think it's an issue with how the objects are being passed to the
> > tag ?
>
> > I'm a bit of a Noob with all this (as you can probably tell !!)
>
> > Thanks
>
> > On Oct 14, 12:16 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
>
> > > On Oct 14, 10:38 am, grimmus <graham.col...@gmail.com> wrote:
>
> > > > Hi,
>
> > > > I have 2 Models, Link and SaveLink.
>
> > > > Link allows URL's with title's and descripions to be added to the
> > > > site.
> > > > SaveLink allows the logged in user to save their favourite links.
>
> > > > When i output all the links on the page i would like to check if the
> > > > active user has already saved the current link.
>
> > > > Something like
>
> > > >         {% if user.is_authenticated and not user.has_saved_link %}
> > > >         <p class="saveContainer" ><a id="{{object.id}}"
> > > > href="{{object.get_save_url}}" class="save">Save</a>
> > > >         {% else if user.is_authenticated and user.has_saved_link %}
> > > >         <p>Already Saved</a>
> > > >         {% else %}
> > > >         <p><a href="/accounts/login/">Login to save</a>
> > > >         {% endif %}
>
> > > > I have not made the user.has_saved_link function yet and am unsure how
> > > > to tackle this. Maybe i could pass the current context back to a
> > > > function and compare it against what the user has already saved ?
>
> > > > Thanks in advance
>
> > > Assuming that 'object' is the link to save, and that there is a
> > > foreign key from SavedLink to Link, your lookup needs to be something
> > > like:
> > >     user.savedlink_set.objects.filter(link=object)
>
> > > So the question is how can we do that lookup in a template. I'd do it
> > > as a filter, since these are easy to write and can be used in an {% if
> > > %} tag.
>
> > > @register.filter
> > > def has_saved_link(user, object)
> > >     return user.savedlink_set.objects.filter(link=object).count()
>
> > > Then you can do:
> > > {% if user.is_authenticated and not user|has_saved_link:object %}
>
> > > --
> > > DR.
>
> Your problem is here:
> has_saved_link = register.tag(has_saved_link)
>
> That should be:
> has_saved_link = register.filter(has_saved_link)
>
> or, as I originally did, use the @register.filter syntax at the top of
> the function. This is the preferred way of doing things except if
> you're on Python 2.3.
> --
> DR.
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to