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 6 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 6 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 1 month ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 2 months ago
  5. Help text hyperlinks by sa2812 1 year, 2 months ago

Comments

Please login first before commenting.