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