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.

Security

All of the views inside djeeterprofile/views.py (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/views.py, 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
-Liking
-Redjeeting
-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
-Security
-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

Comments

Joey

27. jun 2018 07:13

Are you soon posting any followups on this?

Stein Ove Helset

29. jun 2018 07:19

Hi Joey. I'm have started to work on a coupe of more posts, it just takes time. Sign up to the newsletter and you'll get notified when they are posted!

Matt

19. jul 2018 09:09

Thank you for a wonderful tutorial. I'd really like to see how to add Vue.js to djeeter.

Stein Ove Helset

20. jul 2018 05:54

Thanks, Matt!
Adding vue to this might not be a bad idea :-) I will see what I can come up with. Thanks for the tip!

S

27. jul 2018 14:52

is there a demo site, I can view this app on?

Stein Ove Helset

31. jul 2018 06:53

No, sorry there isn't. I have been thinking about setting up one, but haven't got around to do it yet.

Add comment