Skip to content

Instantly share code, notes, and snippets.

@nicpottier
Created April 17, 2010 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicpottier/369682 to your computer and use it in GitHub Desktop.
Save nicpottier/369682 to your computer and use it in GitHub Desktop.
Simple Django template tag to get the image URL for a photologue photo by slug.
This gist is just a simple Django template tag that will output the display
URL for a photologue photo looked up by slug. This satisfies my need for using
Photologue simple as an admin interface to managing photos that I'm including
in flatpages.
Usage
=====
Just put photos.py in your templatetags directory. Make sure photologue
is installed, then create a new photo object.
Optionally, you can install the modified change_form.html admin view below to
change the 'view on site' link to link directly to the image.
Display your photo within a template with the following:
{% load photos %}
...
{% photo_url "photo_slug" %}
<!-- stick this in templates/admin/photologue/photo if you want
to bypass using any of the photologue templates. This will just
change the 'view on site' link in the admin change view to link
directory to the display url. -->
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools %}
{% if change %}{% if not is_popup %}
<ul class="object-tools">
<li><a href="history/" class="historylink">{% trans "History" %}</a></li>
<li><a href="{{ original.get_display_url }}" class="viewsitelink">{% trans "View on site" %}</a>
</li>
</ul>
{% endif %}{% endif %}
{% endblock %}
from django import template
from django.db import models
from photologue.models import Photo
register = template.Library()
Photo = models.get_model('photologue', 'photo')
def photo_url(format_string):
"""Tries to load the appropriate Photologue Photo object by slug, and outputs
the url to the display image. If photo is not found, then returns an empty
string."""
try:
photo = Photo.objects.get(title_slug=format_string, is_public=True)
return photo.get_display_url()
except Photo.DoesNotExist:
return ''
register.simple_tag(photo_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment