Part VIII.b – User Authentication with Twitter using Django Allauth

As an extension of the previous Part VIII, we’re going to take a look at the user authentication using Twitter.

If you didn’t follow this tutorial from the beginning, I recommend that at least, you take a look at the previous Part VIII, as I will skip some steps that are commented there.

The outline of this part is:

Let’s start! 😉

Test First! Test First!

Edit the funcitonal_tests/ file and add the following test for Twitter:

And run it with

which of course… will fail.

Configure Allauth

In order to log in a user with a Twitter account we need to add the following app in

And migrate our database:

And finally, we are going to add a button in the base.html template (like we did with Google). This button will be used by the user to login with Twitter:

However, we don’t have a Twitter application yet! Let’s change that! 😉

Create a Twitter Application

Create a new Twitter Application from here with:

  • Website: (twitter doesn’t allow localhost as an url)
  • Callback Url:

Change this domain by your domain name in production, if applicable.

Next, create an Allauth social application in with

  • Provider: Twitter
  • Name: Twitter (or something similar)
  • Client ID: Your Twitter app Consumer Key (API Key)
  • Secret Key: Your Twitter app Consumer Secret (API Secret)
  • Sites: Select the corresponding site

Both Consumer Key and Secret will be found in the Keys and Access Tokens tab.

Note: As we’ve seen with Google, Django tests run in another domain: Therefore, you’ll have to repeat this process an create another app for testing (Twitter doesn’t allow multiple urls in the same app).

Now it’s time to create the fixtures to use in the database. In settings/, select a dummy database:

This will allow us to create fixtures from this testing database anytime we want to.

Activate your testing environment (to use this settings file) and configure the database:

Modify the name of the default site to here. And create the corresponding social applications here (Google and Twitter for this tutorial).

Stop the server and create the fixture with:

To simulate the login of a user, we’ll write the user credentials in the file fixtures/twitter_user.json as:

where you should use valid credentials. Remember to include the fixtures folder in the .gitignore file, as it contains sensible data.

Finally, run your tests again!

Yes! They worked! 🙂

I also wanted to use Facebook, but they banned me 🙁 I don’t know why I can’t sign up as a developer!


Now you’re ready to continue to next part! Model creation, OneToOne relations, signals and the Django Admin.

Please, +1 and share if useful! Thanks!! 🙂



Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)