Takes a tweet url, requests the json from Twitter oEmbed, parses the json for the html element and returns it to your template. The html returned is ready to go and will be shown as a tweet on your web page. This uses the Requests library for Python. A full example can be found on GitHub https://github.com/z3ke1r/django-tweet-embed.
1 2 3 4 5 6 7 8 9 10 11 | import requests
from django import template
register = template.Library()
@register.simple_tag
def tweet_tags(url):
twtjson = requests.get('https://publish.twitter.com/oembed?url=' + url + '&omit_script=true')
twtparse = twtjson.json()
twthtml = twtparse['html']
return twthtml
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Example:
This will loop through the 'tweets' queryset (defined in the view). Send the URL of each tweet object (
x
) to the tag function and return the embed HTML into the<div card>
in the template.template.html
#
Please login first before commenting.