How to Combine Two Python Decorators
Imagine you have some Django views using the same two decorators:
(All tags.)
Imagine you have some Django views using the same two decorators:
I previously covered using Django’s CheckConstraint class to validate fields with choices and percentage fields that total 100%. Here’s another use case.
Python’s decimal module has concept of a “context”. This defines the default precision of new Decimals, how rounding works, and lots of other behaviour. Maths gets complicated!
Django’s default settings file has always included a BASE_DIR pseudo-setting. I call it a “pseudo-setting” since it’s not read by Django itself. But it’s useful for configuring path-based settings, it is mentioned in the documentation, and some third party packages use it.
I previously covered using Django’s CheckConstraint class to ensure a field with choices is constrained to only valid values. Here’s another use case, based on an application I worked on. It uses a check constraint to ensure a set of fields, representing percentages, always sum up to 100.
If you’re hosting a website on a top level domain, you should set up both the bare domain (example.com) and the “www” subdomain (www.example.com). People expect to be able to type either version and see your site - no matter which version you advertise.
When you run Django’s manage.py makemigrations, it will try to generate a name for the migration based upon its contents. For example, if you are adding a single field, it will call the migration 0002_mymodel_myfield.py. However when your migration contains more than one step, it instead uses a simple ‘auto’ name with the current date + time, e.g. 0002_auto_20200113_1837.py. You can provide the -n/--name argument to makemigrations, but developers often forget this.
Django templates are often used to pass data to JavaScript code. Unfortunately, if implemented incorrectly, this opens up the possibility of HTML injection, and thus XSS (Cross-Site Scripting) attacks.
robots.txt is a standard file to communicate to “robot” crawlers, such as Google’s Googlebot, which pages they should not crawl. You serve it on your site at the root URL /robots.txt, for example https://example.com/robots.txt.
Django provides MySQL and MariaDB support out of the box. It supports the mysqlclient library as its DB API driver to connect.
Here are some issues I’ve seen crop up several times in Django projects using Celery. They probably apply with other task queues, I simply haven’t used them so much.
One of the headline features of Django 3.0 is its Enumerations for model field choices. They’re a nicer way of defining and constraining model Field.choices.
This post is a PSA on the somewhat unintuitive way Field.choices works in Django.
On Wednesday evening last week I held a quiz at the January London Django Meetup Group. This was the fourth quiz, which has become an annual Christmas tradition at the meetup. Unfortunately it was a month late this year due to venue changes, so I’ve titled this post “Django Quiz 4”.
If you use Django’s test runner, you’ll probably have encountered this message:
Bruno Oliveira, known for his work on the pytest project, tweeted this thread last July:
pytest is quickly becoming the “standard” Python testing framework. However it can be overwhelming to new users.
Several large Django applications that I’ve worked on ended up with memory leaks at some point. The Python processes slowly increased their memory consumption until crashing. Not fun. Even with automatic restart of the process, there was still some downtime.
Django 3.0 alpha 1 came out this week. It introduces ASGI support thanks to lots of hard work by Andrew Godwin.
datetime modulePython’s datetime module risks a whole bunch of name confusion:
A few weeks ago I had the pleasure of talking over the internet with Will Vincent and Carlton Gibson about lots of Django-related topics. They somewhat informed me it was being recorded for a podcast.
On several Django projects I’ve worked on, there has been a requirement for performing database modifications beyond Django migrations. For example:
In my blog post and DjangoCon Europe talk earlier this year How to Score A+ for Security Headers on Your Django Website, I covered that Feature-Policy was a “bonus header”. In a recent update, Scott Helme wrote that an A+ on SecurityHeaders.com now requires Feature-Policy. Also it no longer requires X-Xss-Protection (though it’s still a good idea).
This is a story about how I sped up a client’s Django test suite to be three times faster, through swapping the test case class in use.
One thing I often ask for in code review is conversion of tuples to lists. For example, imagine we had this Django admin class: