我正在尝试使用python django和javascript嵌入推文。
这是我目前的views.py:
def tweets(request):
context_dict = {}
tweets_today = Tweet.objects.order_by('-favorites')
tweets_today = tweets_today[:15]
context_dict['tweets_today'] = tweets_today
return render(request, 'summary/tweets.html', context_dict)
还有我的HTML(base.html具有twitter JS脚本)
{% extends 'base.html' %}
{% block body_block %}
<div class="container">
{% for tweet in tweets_today %}
<div class="block social-block social-twitter">
<div id="container_{{ forloop.counter }}">
</div>
<a href="#" class="btn btn-round btn-red"></a>
</div>
<script>
window.onload = (function () {
twttr.widgets.createTweet(
'{{ tweet.tweet_id }}',
document.getElementById('container_{{ forloop.counter }}'),
{
theme: 'dark'
}
);
});
</script>
{% endfor %}
</div>
{% endblock %}
由于某种原因,这仅嵌入了一条推文。是因为window.onload吗?