There are two things I regret not learning properly before starting my first development job: git and testing.
By learning how to write tests, I found my workflow became more efficient as I was spending less time doing manual testing in the browser.
If I was moving chunks of code around or adding new features, running my test suite would alert me right away if my changes had broken anything.
When I was writing tests, it would force me to think of edge-cases and ways that users could break the app. This resulted in fewer bugs in the first place.
I have a confession to make. Until yesterday, I was using print statements to debug my Python code.
Print statements will help you get the job done, but they are cumbersome to use. You need a separate statement for every path your code’s logic could take you and you have to remember to remove them when you’re finished.
Print statements also return information as a string, so what you see is what you get. If you’re debugging something complex, you might want to be able to explore whole objects.
There is an easier way.
Using the debugger will provide you…
Django does not automatically log in users after signing in, but achieving this functionality only takes a few extra lines of code.
I’m using Class-based Views. To automatically log in a user, we have to override the form_valid() method of the CreateView class. The form_valid() method is the last block of code to be run before redirecting the user to the success_url.
Django contrib auth provides two functions: authenticate and login. We can call these methods to log a user in without displaying a login form.
The Django source code is a good place to find out more about the authenticate and login methods:
Yesterday, I published a tutorial on how to build forms with ManyToMany fields. The back-end work was almost complete, except for a bug which we will fix in this post.
I’m currently building a meal planning app. When a user creates a new meal, I want them to have checkboxes to select which household members will be joining the meal. The list of household members will be unique to each user.
In the previous post, I implemented the form functionality using a ModelMultipleChoiceField class and replaced the default widget with checkboxes. You can read more about that here.
Building forms with Django is straightforward if your fields are simple inputs like text fields, but what if your forms need to reference other models?
In this tutorial, we will build a form with a group of checkboxes, where the options come from another model.
I’m currently building a meal-planning app. To give users flexibility, they will have the option to create separate plans for different household members.
These are my models. I have a class called ‘Meal’, which references a class called ‘Member’.
meals/models.pyclass Meal(models.Model): name = models.CharField(max_length=255)
date = models.DateField()
members = models.ForeignKey(Member, on_delete=models.PROTECT)household/models.pyclass Member(models.Model):…
Despite being two months into my Django journey, simple routines like getting a project set up are far from second nature.
git clone https://github.com/<username>/<forked-repo>.git
python3 -m venv venvsource venv/bin/activate
It is important to keep sensitive bits of code like API keys and passwords away from prying eyes. The best way to do this is to not put them on GitHub! Even you’re doing a personal project with no real users, securing your environment variables will build good habits and prevent pesky emails from GitGuardian. Here’s how to do it in Django.
In your terminal, inside the project directory, type:
$ pip install django-environ
Below your import in settings.py:
import environ# Initialise environment variablesenv = environ.Env()environ.Env.read_env()
In the same directory as settings.py, …
I would describe Express as a blank canvas. The boilerplate code will get your development server up and running but not much else. You the developer will then install lots of third-party packages known as ‘middleware’ to get things running the way you like them. Express gives developers lots of freedom and flexibility, but starting a project from scratch is a lot of work.
Django is a framework that…
Attempting to understand Linux comes with a lot of new terminology. As a self-taught developer, I believe learning the foundations of computing is just as important as learning how to code. Today, I’ve been trying to wrap my head around operating systems. Here are some of my notes.
A kernel is the part of the operating systems that facilitates communication between the hardware and the software.
In the words of the Linux Information Project:
The kernel is a program that constitutes the central core of a computer operating system. It has complete control over everything that occurs in the system.
Strapi is a great headless CMS for front-end developers. It’s open-source, flexible, and easy to set up. After endless frustration with headless Wordpress, using Strapi was a breath of fresh air.
Getting set up was easy. Deploying was confusing. I like Heroku because it can host my side-projects for free. However, deploying Strapi to Heroku presented some unique challenges. Here’s what you need to know.
I initially ran Strapi locally on localhost:1337, which was ideal while I developed the Gatsby front-end of my application. …