How To Tweet Using Django

How To Tweet Using Django

/ #Django

Learn how to use a Python library called Tweepy to post tweets to your timeline using Django.

I just installed Django and created an empty project. Feel free to use an existing project if you want to do that.

Now it's time to show you how to use a library called tweepy to tweet using Django. First, we can install the library by running "pip install tweepy".

To use the twitter api, you need some credentials from twitter. Go to Here you need to create an app in order to get the credentials you need. When you're ready, you can continue.

I'm going to create a django app which will be used for tweeting.

$ python startapp post

Let's register this in the file so django knows we will be using it


Let's create a view which can be used for this:

def index(request):
	if request.method == 'POST':
		content = request.POST.get('content', '')

		if content:
			print('Content:', content)

			return redirect('post')

	return render(request, 'post/index.html')

And then a simple template:

<form method="post" action=".">
	{% csrf_token %}

	<input type="text" name="content"><br>


Then we just need to add this to the urls, and we can test it.

from post import views

urlpatterns = [
    path('', views.index, name='index')

If you open up the browser now and submit the form, you would see the contents in the terminal.

Using Tweepy

First, I want to define the api credentials in the file:

# Twitter

API_KEY = 'ruQWMpxFBziKs08KlVrpVsACn'


ACCESS_TOKEN = '1006977823687479297-AoHcTmvEyXYyUUNveTOeKm6IQo0Y1t'


Then we need to import two new things in the file:

import tweepy

from django.conf import settings

If we now just alter the view a little bit, the backend will be ready for tweeting:

if content:
	print('Content:', content)

	auth = tweepy.OAuthHandler(settings.API_KEY, settings.API_KEY_SECRET)
	auth.set_access_token(settings.ACCESS_TOKEN, settings.ACCESS_TOKEN_SECRET)

	api = tweepy.API(auth)

	return redirect('post')



No comments yet...

Add comment


Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.