Login

Template filter to turn Twitter names into links

Author:
mrben_
Posted:
July 14, 2010
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Quick and simple twitterize filter to turn Twitter usernames into profile links on your own sites.

Add the filter code to your own template tags (http://docs.djangoproject.com/en/dev/howto/custom-template-tags/).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# template tags file
import re
from django import template
register = template.Library()

@register.filter(name='twitterize')
def twitterize(token):
    return re.sub(r'\W(@(\w+))', r'<a href="https://twitter.com/\2">\1</a>', token)
twitterize.is_safe = True

# Use in your templates like this:
<ul>
  {% for t in tweets %}
    <li>{{ t.text|urlize|twitterize }}</li>
  {% endfor %}
</ul>

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.