

In my case, however, this was sufficient. With your tweets in the MySQL table it's pretty easy to display them on your website. Of course you can get and save much more information from the Twitter API. To get the tweets on demand (with a click on a button) we extend the views.py again. (tweet_id = pk).update(is_active = True)Īnd also the views.py: tweet_set_inactive(request, pk):

(tweet_id = pk).update(is_active = False) To be able to manage the tweets in the list we extend the twitter.py by two functions. Return render(request, 'cms/tweet_list.html', ĭont't forget to add the corresponding url patterns for all views in the urls.py. Tweets = paginator.page(paginator.num_pages) For this purpose, we display the stored tweets in a list and give the option of marking them as "inactive". To do this, we first extend the views.py. New_tweet = Tweet(tweet_id = original_tweet.id, tweet_text = original_tweet.text, published_date = original_tweet.created_at, is_active = True)Īs already described, we want to control which timeline tweets are displayed on the website. User_tweets = api.user_timeline(count=50) Therfore we use a simple Python script " twitter.py": import tweepyĪuth = OAuthHandler('Consumer Key', 'Consumer Secret')Īuth.set_access_token('OAuth Access Token', 'OAuth Access Token Secret') Let's get the tweets from the user timeline and save them in the database table.

How to do this is described in the following article: How to Register a Twitter App in 8 Easy Steps To get these keys you need to register a Twitter app first. To access the Twitter API with Tweepy you need a Consumer Key and a Consumer Secret as well as an OAuth Access Token and an OAuth Access Token Secret. Tweepy is a very powerful Python library for accessing Twitter. Return self.tweet_text Get the Tweets using Tweepy Is_active = models.BooleanField(default=True) Published_date = models.DateTimeField(blank=True, null=True) Tweet_id = models.CharField(max_length=250, null=True, blank=True) It's a very simple table with only a vew columns. The Tweet Tableįirst let's create a new table in the database, in my case it's a MySQL database. I wanted to display my current tweets on the homepage of my website but also needed the possibility to choose which tweets should be displayed and which not. So I decided to save the tweets first in a simple MySQL table and then manage them in the backend. You want to display your Twitter messages on your Django website and decide which tweet should be displayed? No problem, here you can find my solution.
