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.


The fastest way to squash those bugs

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.

Notes

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.

Some context:

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.

The Problem

The ModelMultipleChoiceField…


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.

Building forms with ManyToMany

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):


One of the things I miss about my JavaScript days is the ease of cloning a repository and getting the dependencies installed. JavaScript projects tend to have a file called package.json which has all the dependencies listed. Simply running $ npm install from the command line was enough to get going.

Despite being two months into my Django journey, simple routines like getting a project set up are far from second nature.

1. Clone the repository

git clone https://github.com/<username>/<forked-repo>.git

2. Create your own virtual environment

python3 -m venv venvsource venv/bin/activate

Virtual environments are where dependencies are stored, similar to node_modules in JavaScript. …


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.

1. Install Django Environ

In your terminal, inside the project directory, type:

$ pip install django-environ

2. Import environ in settings.py

import environ

3. Initialise environ

Below your import in settings.py:

import environ# Initialise environment variablesenv = environ.Env()environ.Env.read_env()

4. Create your .env file

In the same directory as settings.py, …


When you learn a back-end framework, you become used to doing projects in a certain way. My first framework was Express (the E in MERN stack), a very minimalist JavaScript framework.

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.

Linux is a kernel, not an operating system

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.

1. Don’t spend too much time adding content locally

I initially ran Strapi locally on localhost:1337, which was ideal while I developed the Gatsby front-end of my application. …

Alice Campkin

Full-stack developer and Django enthusiast. When I can’t find Django tutorials, I write them. https://twitter.com/alcampk

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store