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.