使用python-Django+Javascrip嵌入tweet

2024-04-20 03:42:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用pythondjango和javascript嵌入tweet。你知道吗

这是我的视图.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(基本.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 %}

出于某种原因,这只是嵌入一条tweet。是因为window.onload?你知道吗


Tags: dividtodayrequestcontainerhtmlcontextsocial