I Found the Best Django Tutorial

So as I mentioned recently, I’ve decided to work a little more on the basics of Django since I felt like I was missing a few things. I know I can just continue with my blog project, doing things any way I can make them work, but it really bothers me to know that I’m not doing things properly. I’m always after the best way of doing things.

People say that it doesn’t matter how bad your code is; you should just try to build something and then you get better. Maybe that’s true, but I just hate working on something and all the while thinking ‘the way I am doing this is so wrong’. I guess I’m a bit of a perfectionist in some ways and it isn’t always to my benefit.

So I found a few Django tutorials I was interested in. I specifically made sure they used at least Django 2, since I feel that any version earlier than that is going to be too different and I’d rather learn what is current (the current version of Django is 2.2.3).

I completed the official Django tutorial the other day. I did learn a few things I hadn’t known previously, but still didn’t really feel confident. It was a good tutorial but I wanted something that was a little more in-depth.

Now I’ve almost finished Corey Schafer’s YouTube series, which takes you through building a multi-user blog website. This is going to go straight to the highest spot on my resources page for Django because it is awesome. He explains everything really well, including the little pieces that other tutorials kind of just ignore. I hate it when someone makes a tutorial that they claim is aimed at beginners, but then don’t explain everything. It’s lazy to just say ‘okay copy and paste this code, but don’t worry about what it does’.

I should note here that’s it’s fine to clarify that you don’t have to worry about what it does now, and then bring it up again later in the tutorial. But to just never explain it is bad and people who write ‘beginner’ tutorials like that should be ashamed.

Additionally, Corey’s tutorials introduced me to some awesome add-ons such as Crispy Forms, which is some kind of magic that makes your forms look good and provide useful feedback to users depending on their input. (Okay, it’s probably not really magic but it definitely seems like it.) It is super easy to use. You just have to install it:

pip install django-crispy-forms

Add it to your settings.py as an installed app:

INSTALLED_APPS = [
    'django.contrib.admin',
    ...
    'crispy_forms',
]

As of the time the tutorial was made, Crispy Forms uses Bootstrap 2 by default. To make sure it uses Bootstrap 4 (the current version), also put this line in your settings.py file:

CRISPY_TEMPLATE_PACK = 'bootstrap4'

Then whenever you have a template with a form, you can just load crispy forms like this:

{% load crispy_forms_tags %}

And then add the filter to your form:

{{ form|crispy }}

There are a bunch of other interesting tutorials on his channel too. I’ll probably check out the Python and Flask ones next.

I’m now thinking of adding a few extras to my blog project, like incorporating the polls app I made by following the official Django tutorial, since I think that would fit in. I think one big thing that has made me hesitant to add more is lacking an understanding of how apps work within Django. The tutorials I’ve followed before all had only one, so I wasn’t sure how to make different apps work together within one site. I mean, I had ideas, but I always prefer to see things in practice. Pretty early on in Corey’s tutorial he made me start a second app, and I was instantly thrilled that I would get some experience with a multi-app website.

As well as taking you through building a good example of a basic blog website, there are a few videos at the end of the series that guide you through deployment, something which I think is often not covered in enough detail considering what a pain it can sometimes be.

If you follow all the videos after building the website, then you will learn how to deploy to both a Linux server and Heroku, as well as how to configure a custom domain name, enable HTTPS, and set up a AWS S3 for file uploads.

This is the part I’m up to now, and I’ll likely watch the videos but not actually deploy since I’ve already done that with Heroku and I’m happy enough with that for now. I’ve used S3 before too, and I’ve been thinking of hosting my static files in an S3 bucket since I’ve heard that’s a thing people do because it’s apparently super cost-effective.

I’d also be working with AWS if I get the job I’m preparing for so when there’s a chance to incorporate those services in any way then I feel I should take it. But enough about AWS for now.

If anyone is looking for a super easy to follow Django tutorial that actually explains why things are done a certain way then I’d recommend you start here. I have a few more resources I want to take a look at, but I really don’t see them being as in-depth as this course. I’ll update here if I find anything else amazing though, and anything else that I at least find worth doing will be added to my resources page.

Doing Things the ‘Right’ Way and Learning Frustration

I always tend to get hung up on this when making my own projects. In my experience it’s usually much easier to do things properly at the beginning, than go back and fix everything you were doing wrong later. It’s also hard to change when you’ve fallen into a bad habit. But when so many people have different ideas of what the ‘right’ way is, it can be really difficult to know for sure.

For example, while doing some reading after finishing the Django Girls tutorial, I found that many people thought it was better to use class-based views. The tutorial did not have us writing class-based views. The more I read, the more I felt that was how it should be done. So I went to my code and changed about half the views to use classes. The others would have been more difficult to convert. And I realized that, although class-based views can definitely be more efficient if you’re doing something simple, they shouldn’t necessarily be used for everything. Sometimes writing your own function is better.

I hadn’t changed anything on my Django blog project for a few days because I wanted to add categories and tags, and I wasn’t sure what the best way was to implement them. I ended up following the example set by the Mozilla tutorial, which has you create a book catalog for a library, and added a Category model which was also a ManyToManyField in the Post model. This is the same way they implemented a genre for books and I figured they were fields that worked the same way – a book can have multiple genres and a blog post can have multiple categories.

I then ran into the problem of how I was going to display a list of categories on every page. I wanted to add it to my base template, but I couldn’t think of a simple way to do that. It turns out that writing a context processor was the answer. All I had to do is make a file called ‘context_processors.py’ (or maybe whatever you want, but this is the usual name) and enter this:

from .models import Category

def categories_processor(request):
    categories = Category.objects.all()
    return {'categories': categories}

This allowed me to easily add categories to my base template with a simple for loop to display the name:

<h3>Categories</h3>
    <ul>
        {% for category in categories %}
        <li> {{ category }} </li>
        {% endfor %}
    </ul>

However, I then had the issue of not knowing how I could make those categories have a count next to them to show how many posts fall into that category. I also didn’t know how I could link to a filtered post list when you clicked a category’s name. I mean, I had an idea of how it could work, but it would involve making a new view and url for each filter (there’d be a tag one soon too), and that just didn’t feel right to me. What I want is like, one view, that takes a parameter that is the filter, and then displays the posts according to that filter, using the same url and the same template. But I couldn’t seem to work out how that should be done.

I did some research, and maybe django-filter is my best option, but I’m just not really clear on how it works. I thought that maybe I should do a bit more basic Django practice since there’s still a lot that I don’t know. So far, all I’ve done are the Mozilla and Django Girls tutorials. While the Django Girls one was good, it was very basic. And the Mozilla one was more complex, but I actually did that one first, and I think a lot of it went over my head. They often told you to just ‘copy and paste this’ – though I very rarely do that when following tutorials as I learn better from typing myself – and I don’t feel they explained enough. My feeling is that when you are teaching someone something that they know nothing about, you should explain as much as possible, especially when it’s something as big as Django. I was going to use the word complex there but I’m not sure if that’s accurate, I mean I think that once you know what you’re doing you can probably use it to make websites pretty easily. But there’s a lot going on there and I wish there were more tutorials written like the Django Girls one where things are broken down in a very simple way.

So anyway, I think I’m putting my blog project on hold until I understand more about what I’m doing. I’m going to complete the official Django tutorial and then watch some Youtube tutorials that have been on my list for a while now.

I know people often say that you shouldn’t get stuck in a cycle of completing beginner tutorials over and over, but for me personally, I feel that by creating a project, even when I’m following along with a tutorial, I learn a lot. Anything that I already know is reinforced, and it also shows the different ways people do things when it comes to coding. Which brings me back to my initial point – I want to do things the right way, and the more exposure I get to different methods, the more likely I am to figure out what that is.

In addition to this I’ll continue to do coding challenges every day, which I have been for the past month or so. I also saw that apparently The Odin Project has just released a JavaScript full stack course, so I’m going to check that out. Previously I hadn’t bothered much with their course because it initially only taught Ruby on Rails, and I was focusing on Python. While I’m not a huge JavaScript fan, I know it’s useful, but what I know is fairly basic. The job I hope to apply for soon will require it so it would probably be a good idea to work on those skills.

That’s the other thing – I’m not sure I’d be using Django at all if I get this job. I just started it and liked it. I frustratingly don’t have all the details, but I was told I have to be strong in Python and have a passing knowledge of Node.js. This is pretty vague to me but it leads me to believe that they use some Node framework like perhaps Express on the backend and use Python alongside it for scripting, or something. I wish I knew more, since it would be much easier to study if I did.

Oh well, I’m going to go and hopefully learn something now.