Created
April 17, 2010 17:03
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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