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 <[email protected]> wrote:
> On Oct 14, 10:38 am, grimmus <[email protected]> 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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to