RSSAmplifier

Blog

Matt's Dev Blog

mattsegal.devRSS feed ↗57 posts

Latest posts

How I hunt down (and fix) errors in production

Once you’ve deployed your web app to prod there is a moment of satisfaction: a brief respite where you can reflect on your hard work. You sit, adoringly refreshing the homepage of www.mysite.com to watch it load over and over. It’s beautiful, perfect, timeless. A glittering …

How to setup Django with Pytest on GitHub Actions

Someone recently asked me When is a good time to get automated testing setup on a new Django project? The answer is "now". There are other good times, but now is best. In this post I'll briefly make my case for why, and show you an example of a minimal …

My (free) Django monitoring stack for 2022

You've built and deployed a website using Django. Congrats! After that initial high of successfully launching your site comes the grubby work of fixing bugs. There are so many things that can will go wrong. Pages may crash with 500 errors in prod, but not locally. Some offline tasks never …

DevOps in academic research

I'd like to share some things I've learned and done in the 18 months I worked as a "Research DevOps Specialist" for a team of infectious disease epidemiologists . Prior to this job I'd worked as a web developer for four years and I'd found that the day-to-day had become quite …

How to compress images for a webpage

Often when you're creating a website, a client or designer will provide you with large images that are 2-5MB in size and thousands of pixels wide. The large file size of these images will make them slow to load on your webpage, making it seem slow and broken This video …

How to setup Django with React

It's not too hard to get started with either Django or React. Both have great documentation and there are lots of tutorials online. The tricky part is getting them to work together. Many people start with a Django project and then decide that they want to "add React" to it …

How to highlight unused Python variables in VS Code

I make a lot of stupid mistakes when I'm working on Python code. I tend to: make typos in variable names accidently delete a variable that's used somewhere else leave unused variables lying around when they should be deleted It's easy to accidentally create code like in the image below …

A Django project blueprint to help you learn by doing

There's an awkward point when you're learning Django where you've done the official tutorial and maybe built a simple project, like a to-do list, and now you want to try something a little more advanced. People say that you should "learn by building things", which is good advice, but it …

Django project blueprint: data model

This post is an appendix to my post on designing a Django project . In this page I explain why I chose to use this data model: I created this data model by looking at the user journeys and thinking about what data I would need to make them work. Here's …

Django project blueprint: wireframes

This post is an appendix to my post on designing a Django project . This page shows all the wireframes for the app, with some additional notes for each page. Page designs for the user who answers the survey This section covers the pages required for the "survey taker" user journey …

How to use both camelCase and snake_case in your frontend and backend

Python uses snake_case variable naming while JavaScript favours camelCase . When you're buiding an web API with Django then you'll be using both langauges together. How do you keep your styles consistent? You could just use one style for both your frontend and backend, but it looks ugly. Perhaps this …

A breakdown of how NGINX is configured with Django

You are trying to deploy your Django web app to the internet. You have never done this before, so you follow a guide like this one . The guide gives you many instructions, which includes installing and configuring an "NGINX reverse proxy". At some point you mutter to yourself: What-the-hell is …

How to manage logs with Django, Gunicorn and NGINX

So you want to run a Django app using NGINX and Gunicorn. Did you notice that all three of these tools have logging options? You can configure Django logging , Gunicorn logging , and NGINX logging . You just want to see what's happening in your Django app so that you can fix …

How to make your Django project easy to move and share

You need your Django project to be portable. It should be quick and easy to start it up on a new laptop. If it isn't portable, then your project is trapped on your machine. If it gets deleted or corrupted, then you've lost all your work! This issue comes up …

Is Django too slow?

Does Django have "bad performance"? The framework is now 15 years old. Is it out of date? Mostly, no. I think that Django's performance is perfectly fine for most use-cases. In this post I'll review different aspects of Django's "performance" as a web framework and discuss how you can decide …

There's no one right way to test your code

Today I read a Reddit thread where a beginner was stumbling over themself, apologizing for writing tests the "wrong way": I'm now writing some unit tests ... I know that the correct way would be to write tests first and then the code, but unfortunately it had to be done this …

How to find what you want in the Django documentation

Many beginner programmers find the Django documentation overwhelming. Let's say you want to learn how to perform a login for a user. Seems like it would be pretty simple: logins are a core feature of Django. If you google for "django login" or search the docs you see a few …

How to pull production data into your local Postgres database

Sometimes you want to write a feature for your Django app that requires a lot of structured data that already exists in production. This happened to me recently: I needed to create a reporting tool for internal business users. The problem was that I didn't have much data in my …

How to polish your GitHub projects when you're looking for a job

When you're going for your first programming job, you don't have any work experience or references to show that you can write code. You might not even have a relevant degree (I didn't). What you can do is write some code and throw it up on GitHub to demonstrate to …

How to generate lots of dummy data for your Django app

It sucks when you're working on a Django app and all your pages are empty. For example, if you're working on a forum webapp, then all your discussion boards will be empty by default: Manually creating enough data for your pages to look realistic is a lot of work. Wouldn't …

How to automatically reset your local Django database

Sometimes when you're working on a Django app you want a fresh start. You want to nuke all of the data in your local database and start again from scratch. Maybe you ran some migrations that you don't want to keep, or perhaps there's some test data that you want …

How to automate your Postgres database backups

If you've got a web app running in production, then you'll want to take regular database backups , or else you risk losing all your data. Taking these backups manually is fine, but it's easy to forget to do it. It's better to remove the chance of human error and automate …

An introduction to cloud file storage

Sometimes when you're running a web app you will find that you have a lot of files on your server. All these files will start to feel like a burden. You might worry about losing them all if the server fails, or you might be concerned about running out of …

How to backup and restore a Postgres database

You've deployed your Django web app to to the internet. Grats! Now you have a fun new problem: your app's database is full of precious "live" data, and if you lose that data, it's gone forever. If your database gets blown away or corrupted, then you will need backups to …

A tour of Django server setups

If you haven't deployed a lot of Django apps, then you might wonder: how do professionals put Django apps on the internet? What does Django typically look like when it's running in production? You might even be thinking what the hell is production ? Before I started working a developer there …

Studying programming: where to start

You have zero programming knowledge and you want to start learning to code. Where do you start? Maybe you want to learn enough to get yourself a coding job, or you're planning to study computer science in the future and you want to try it out before you start your …

Studying programming: pace yourself

You can learn programming all by yourself and get a coding job. Just you, your laptop and the internet. It's great! You don't have to pay thousands of dollars for a degree and you can work at your own pace. There's a problem with this approach though: with no teacher …

Studying programming: tools or theory?

When you're studying web development you have a lot to learn and limited time. One of the hard choices that you'll need to make is whether you learn tools or concepts. Should you study data structures and algorithms to be a web developer? It seems kind of esoteric. Do you …

How to diagnose and fix slow queries with Django Debug Toolbar

Your Django views are running slowly and you want to make them faster, but you can't figure out what the issue is just by reading the code. Just as bad is when you're not sure if you're using the Django ORM correctly - how can you know if the code you …

Studying programming: what to learn next?

A lot of people trying to teach themselves programming have an anxiety about what they should be learning. There is an endless array of options - you've seen these ridiculous lists of online courses , right? There's too much to learn and not enough time! You don't want to waste time learning …

Keeping your config files valid with Python

It's common to use a config file for your Python projects: some sort of JSON or YAML document that defines how you program behaves. Something like this: # my-config.yaml num_iters : 30 population_size : 20000 cycle_type : "long" use_gpu : true plots : [ population , infections , cost ] Storing config in a file …

8 helpful tools for programming on Windows

Software development on Windows can be a pain. Not because of any issues with C#, .NET or the operating system, but simply because the tools surrounding your work can be quite clunky by default. I'm talking about the lack of a package manager, PowerShell's ugly blue terminal with no tabs …

Run your Python unit tests via GitHub actions

You've written some unit tests for your Python app. Good for you! There are dozens of us, dozens! You don't always remember to run your tests, or worse, your colleagues don't always remember to run them. Wouldn't it be nice to automatically run unit tests on every commit to GitHub …

Simple Django deployment part six: domain setup

We're very nearly done deploying our Django app. There's just one more thing we should take care of. Having a raw IP as our website address is kind of yucky, isn't it? You're not going to ask your friend, boss, or mum to visit 23.231.147.88 to check …

Simple Django deployment part five: deployment automation

Deploying our Django app involved a lot of different commands, right? It would suck to have to do all that over again, wouldn't it? Having to manually type all those commands again would be tedious, slow and easy to screw up. Even worse, the harder it is to deploy, the …

Simple Django deployment part four: run a service

So we've got a problem. Our Django app only runs when we're logged into the server via SSH and running Gunicorn. That's not going to work long term. We need to get Gunicorn running even when we're not around. In addition, if our Gunicorn server crashes because of some bug …

Simple Django deployment part three: deploy code

We've got our server set up, and our Django code is ready. Now we can actually deploy Django to our server. The goal of this section is to get a basic deployment done. We'll do some automation and introduce some extra tools later. In this section we'll cover: Windows line …

Simple Django deployment part two: local setup

We've got our server set up and ready to host our Django app, now let's focus on preparing our app for deployment. The goal of this section is to set up and test as much of the stuff that we'll be using in production. That way, we can debug issues …

Simple Django deployment part one: infrastructure

In order to deploy our Django app, we need a somewhere to run it: we need a server. In this section we'll be setting up our server in "the cloud". Doing this can be fiddly and annoying, especially if you're new, so we want to get it right first before …

Simple Django deployment: a guide

You're learning web development with Django. You've followed the official introductory tutorial and you can get a Django app working on your local computer. Now you want to put your web app onto the internet. Maybe it's to show your friends, or you actually want to use it for something …

Never think about Python formatting again

At some point you realise that formatting your Python code is important. You want your code to be readable, but what's the right way to format it? You recognise that it's much harder to read this: some_things = { "carrots" : [ 1 , 2 ], "apples" :[ 3 , 3 , 3 ], "pears" : [] } than it is to …

Cloudflare makes DNS slightly less painful

When you're setting up a new website, there's a bunch of little tasks that you have to do that suck . They're important, but they don't give you the joy of creating something new, they're just... plumbing. In particular I'm thinking of: setting up your domain name with DNS records encrypting …

Nand to Tetris is a great course

Everyone who learns programming at some point stops and asks - how does this actually work? You might know how to write and run code, but what's actually happening inside the computer ? It can seem unfathomable. Some people don't care about what's happening under the hood. Their code works, it gets …

DNS for beginners: how to give your site a domain name

You are learning how to build a website and you want to give it a domain name like mycoolwebsite.com. It doesn't seem like a real website without a domain name, does it? How is anybody going to find your website without one? Setting up your domain is an important …

4 tips for debugging in Django

You've got a bug in your Django code and you can't quite figure out what's wrong. You know there's a problem, but you can't quite pin down where it's coming from. This post will share 4 tips which will help you speed up your bug catching. Dig deeper in your …

Quickly fix bugs in Django with Python's debugger

There's a bug in your Django code. You've tried to track down the problem with "print" statements, but it's such a slow, tedious process: Add a "print" statement to your code Refresh the page in your browser to re-run your code Look at the runserver console output for the "print …

How to view Django logs with Papertrail

You have a Django app running on a webserver and hopefully you're writing your logs to a file . If anything goes wrong you can search back through the logs and figure out what happened. The problem is that to get to your logs, you have to log into your server …

How to save Django logs in production

You've deployed Django to a webserver and something has broken. There's an error somewhere . What happened? When you're debugging Django on your local computer, you can just throw a print statement into your code and check the output in the runserver logs. What about in production? Where do the logs …

How to customise a class based view in Django

You've spend a little bit of time working on your Django app and you want to dip your toes into class-based views. The basic examples are simple enough, but once you want to do something more complicated, something more custom, you get stuck. How do you customise a class-based view …

9 commands for debugging Django in Docker containers

You want to get started "Dockerizing" your Django environment and you do a tutorial which shows you how to set it all up with docker-compose. You follow the listed commands and everything is working. Cool! A few days later there's an error in your code and you want to debug …