Djitter - how to build a twitter clone using Django 2.0 - Part nine

Djitter - how to build a twitter clone using Django 2.0 - Part nine

Published 18. jun 2018 12:14 by Stein Ove Helset

In the last part of this tutorial series we are going to refactor some of the code and look at some optimization techniques.

This part will be a little bit more step by step where I show you what you do and then I'll add a little explanation why I do what I do.

Moving templates to a better location

Create two new folders.

$ mkdir djeeterprofile/templates
$ mkdir djeet/templates

Move the templates file to the correct folder

$ mv templates/users.html djeeterprofile/templates/
$ mv templates/profile.html djeeterprofile/templates/
$ mv templates/feed.html djeet/templates

Now we have moved the necessary tamplates to a location that makes a little bit more sense. Django will automatically look inside the templates folder since we added this in one of the first parts of this tutorial series.


All of the views inside djeeterprofile/ (except frontpage) should have the @login_required decorator so that if you try to go to a view you'll be sent to the sign in page. The same goes for the feed view inside djeet/, here you'll also need to import the login decorator.

There is problably a lot more we can do to improve the security here, but this is not in the scope of this tutorial.

Optimize the follows / followed by query

The follows and followed by views will be very slow when a user follows or is followed by a lot of djeeters. This is because when we are selecting this way (djeeterprofiles = user.djeeterprofile.follows.all()) we will do an additional query for each djeeterprofile to get his user account. But if we change it to this.

def follows(request, username):
  djeeterprofiles = user.djeeterprofile.follows.select_related('user').all()

def followers(request, username):
  djeeterprofiles = user.djeeterprofile.followed_by.select_related('user').all()

That problem will be gone. When we add that select_related('user') the user account for the djeeterprofile will be selected using a join in the SQL and that's light years faster than it was before.

That's it for this time

The tutorial is now finished and you should have a working clone of a very simplified twitter. There is an endless amount of functions you can start to implement on this project now. We're thinking about expanding this tutorial and here are some of the features we might implement.

-Changin some of the views into class based view for better readability
-Breadcrumbs so it's easier to navigate
-Direct messages to other djeeters
-Mentions of other djeeters
-Attach images to djeets
-A simple search
-Better styling
-Do more refactoring by moving the urls to separate files
-Get notifications when someone follows you
-Get notifications when someone mentions you
-Get notifications when someone redjeets you
-Adding Vue or another javascript framework for more responsiveness
-How to deploy to a production server

What do you want to see in this tutorial as we go on?

If you've got any questions your are more than welcome to leave a comment and I'll answer as soon as possible.

Share this post


No comments yet

Add comment