在django中显示tweepy python文件中的数据

在django中显示tweepy python文件中的数据,python,django,django-models,django-rest-framework,tweepy,Python,Django,Django Models,Django Rest Framework,Tweepy,我一整天都在和他鬼混。我把它放在一个.py文件中,但是我想显示我从tweepy获得的twitter数据,以便在表中显示信息。我对这一点相当陌生,我不确定在django环境中映射testingtweepy.py文件的体系结构是什么样的。以下是我试图在Django中显示为testingtweepy.py的代码: import tweepy from tweepy.auth import OAuthHandler auth = OAuthHandler(consumer_key, consumer_

我一整天都在和他鬼混。我把它放在一个.py文件中,但是我想显示我从tweepy获得的twitter数据,以便在表中显示信息。我对这一点相当陌生,我不确定在django环境中映射testingtweepy.py文件的体系结构是什么样的。以下是我试图在Django中显示为testingtweepy.py的代码:

import tweepy
from tweepy.auth import OAuthHandler

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

public_tweets = api.home_timeline()
for tweet in public_tweets:
    print(tweet.text)
我们的目标是从公共推文中获取数据,并将其存储在Django数据库中,以便我将来可以进一步显示数据


谢谢你的帮助

使用API相当简单。您不需要创建任何模型或表单,除非您希望保存响应数据

  • views.py中创建视图

    def home_timeline(request):
        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
    
        api = tweepy.API(auth)
    
        public_tweets = api.home_timeline()
    
        return render(request, 'public_tweets.html', {'public_tweets': public_tweets})
    
    url(r'^home_timeline/$',views.home_timeline, name='home_timeline')
    
  • 创建html模板
    public\u tweets.html

    <html>
      <body>
        {% for tweet in public_tweets %}
          <p>{{ tweet.text }}</p>
        {% endfor %}
      </body>
    </html>
    

  • 请阅读django中的视图和模型。如果我想将响应保存到数据库,我应该如何做?