215 Posts Tagged ‘django’ (Page 4)

(All tags.)


Django: Flush out test flakiness by randomly ordering QuerySets

Sometimes code depends on the order of a QuerySet whilst not specifying an order. This can lead to random, flaky test failures because databases can return rows in any order when none is specified. The problem is made worse by some databases, notably PostgreSQL, which nearly always return rows in insert order, but occasionally use a different order when a table has had recent deletions.

Read more...

Django: A version of json_script for pre-serialized JSON strings

Django’s json_script template filter is a convenient and safe way to pass a larger amount of data to JavaScript. I covered it in my post last year How to Safely Pass Data to JavaScript in a Django Template.

Read more...

Django: A security improvement coming to format_html()

Can you spot the problem with this Django snippet?

Read more...

Django: Avoid database queries in template context processors

Django’s template engine allows you to augment template contexts with context processors. These are functions that take the current request and return a dictionary to be merged into the context:

Read more...

Django: Parametrized tests for all model admin classes

Here’s an application of “test smarter, not harder”, as per Luke Plant’s post. I came up with this recently whilst working on my client Silvr’s project, and I’m pretty proud of it. It should apply to any project using Django’s admin.

Read more...

Django: How to profile and improve startup time

Your Django project’s startup time impacts how smooth it is to work with. Django has to restart your project every time you run a management command and when runserver reloads. This involves importing all your apps, and thus all the modules that they import.

Read more...

How to migrate from Django’s PostgreSQL CI Fields to use a case-insensitive collation

If you upgrade to Django 4.2, you may see system check warnings like:

Read more...

django-upgrade release with Django 4.2 fixers

I just released django-upgrade 1.13.0. The headline features are some new fixers targeting Django 4.2, which is currently in alpha.

Read more...

Django settings patterns to avoid

Here are some potential mistakes made with Django settings that you can avoid.

Read more...

Django-related Deals for Black Friday and Cyber Monday 2022

Here are some Django-related deals for this year’s Black Friday (25th Nov) and Cyber Monday (28th Nov), including my own. For more deals on general Python-related products, see Trey Hunner’s post.

Read more...

Python: unittest’s new context methods from Python 3.11 (with backports)

Python 3.11 only made one change to unittest, but it’s a good one: context manager methods. These methods can simplify setup and teardown logic in many cases, such as dynamic use of unittest.mock.

Read more...

django-upgrade Mega Release 1.11.0

I just released version 1.11.0 of django-upgrade, a tool for automatically upgrading your Django project code. This release contains a lot of new features and fixes, thanks to new contributors including those at the Djangocon Europe sprints. Let’s look at the top changes.

Read more...

Migrate PostgreSQL IDs from serial to identity after upgrading to Django 4.1

The Django 4.1 release notes feature this short, innocent-looking note:

Read more...

How to implement a “dry run mode” for data imports in Django

In data import processes it’s often useful to have a “dry run” mode, that runs through the process but doesn’t actually save the data. This can allow you to check for validity and gather statistics, such as how many records already exist in the database. In this post, we’ll look at how to implement a dry run mode in Django by using a database transaction and rolling it back.

Read more...

How to Safely Pass Data to JavaScript in a Django Template

You want to pass your data from your Django view to JavaScript, in your template. And, you want to do it securely, with no risk of accidentally allowing malicious code injection. Great, this is the post for you!

Read more...

Django: use partial() with transaction.on_commit() to avoid late-binding bugs

Django’s transaction.on_commit() allows you to run a function after the current database transaction is committed. This is useful to ensure that actions with external services, like sending emails, don’t run until the relevant data is definitely saved.

Read more...

Django: Run a migration “by hand”

Normally your Django project’s deploy process runs the migrate command, and that takes care of updating your database as necessary. Especially on smaller databases, Django’s migration system can “just do it” for you.

Read more...

How to Patch Requests to Have a Default Timeout

Python’s requests package is very popular. Even if you don’t use it directly, it’s highly likely one of your dependencies does.

Read more...

How to optimize PostgreSQL queries from Django using pgMustard

Slow queries happen, and when they do, it can be tough to dissect why they’re slow. This difficulty is compounded by using Django’s ORM, since it generates the SQL for you, so you may have little idea of the actual queries “under the hood”.

Read more...

Django: how to be a teapot

HTCPCP, or Hyper Text Coffee Pot Control Protocol, was published as an April Fool’s joke 24 years ago today. It’s an HTTP extension for controlling coffee pots, and whilst it is a joke, it appears in various places around the web.

Read more...

Django: Raise an error for missing template variables

It’s all too easy to forget to pass a variable to your template or make a typo in a variable name. Unfortunately, it can be quite hard to debug such mistakes, since Django’s default behaviour is to ignore the problem and render an empty string.

Read more...

You probably don’t need Django’s get_user_model()

Django’s authentication system, django.contrib.auth, provides a built-in User model class. You can swap this to a different class with the AUTH_USER_MODEL setting, most easily at the start of the project.

Read more...

django-htmx Now on Read the Docs

I’ve just released django-htmx 1.9.0. As part of this release, it now has a documentation site built with Sphinx, hosted on Read the Docs at django-htmx.readthedocs.io.

Read more...

Announcing WhiteNoise 6

WhiteNoise is a simple solution to serving static assets. It lets you skip configuring a separate web server for your static assets, and serve them straight from Django. It’s a brilliant tool. I’ve used it for years on various projects, and I’ve recommended it many times, and mentioned it in several blog posts.

Read more...

Django: serve source maps with contrib.staticfiles

Source maps are files that map your minified CSS or JavaScript back to the original code. They allow you to use your browser’s development tools to debug minified code as if it were the original. Also some error capture tools, such as Sentry, can use source maps to report errors for the original code.

Read more...