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/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
-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
-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.