- Author:
- z3ke1r
- Posted:
- February 14, 2019
- Language:
- Python
- Version:
- 2.1
- Tags:
- django templatetag json embed twitter parse requests tweet
- Score:
- 0 (after 0 ratings)
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
|
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.