django-fastdev
is an app that makes it safer, faster and more fun to develop Django apps.
Django templates by default hide errors, and when it does show an error it's often not very helpful. This app changes this behavior to provide more informative error messages. For example, if you use:
{{ does_not_exist }}
instead of rendering that as an empty string, this app will raise a FastDevVariableDoesNotExist
error with a detailed message:
does_not_exist does not exist in context. Available top level variables: DEFAULT_MESSAGE_LEVELS False None True bar csrf_token foo messages perms request user
There are more specialized error messages for accessing non-existent keys in a dict
or attributes of an object several levels deep, such as foo.bar.baz
(where baz
doesn't exist).
Handling `default` and `default_if_none` Filters
When using the default
or default_if_none
filters, django-fastdev
will not raise an exception for non-existent variables. Instead, it behaves as one might intuitively expect by populating the context variable with the result of the filter operation. For example:
{{ does_not_exist|default:"N/A" }}
{{ does_not_exist|default_if_none:"" }}
- In these cases:
- If
does_not_exist
is undefined,default:"N/A"
will render asN/A
, anddefault_if_none:""
will render as an empty string (""
). - This ensures that templates using these filters handle missing variables gracefully, aligning with Django's built-in behavior while maintaining
django-fastdev
's strict checking for other cases.
- If
By default, django-fastdev
only checks templates that exist within your project directory. To check ALL templates, including stock Django templates and templates from third-party libraries, add FASTDEV_STRICT_TEMPLATE_CHECKING = True
to your project settings.py
.
Good suggestions for what you wanted to do, and a complete list of all valid values makes it very easy to fix TemplateDoesNotExist errors.
Have you ever gotten this error?
django.urls.exceptions.NoReverseMatch: Reverse for 'view-name' with arguments '('',)' not found. 1 pattern(s) tried:
It's because you have {% url 'view-name' does_not_exist %}
. Django sees
does_not_exist
and evaluates it to the empty string because it doesn't exist.
So that's why you get an error message that makes no sense. django-fastdev
will
make your code crash on the actual error: does_not_exist
doesn't exist.
A common mistake for beginners that can be hard to spot is when they do:
<html>
{% extends "something.html" %}
stuff here
</html>
Django silently throws away stuff here
and </html>
. django-fastdev
makes this an error.
If you have a base template:
<html>
<body>
{% block content %}{% endblock %}
</body>
</html>
and then write a template like this:
{% extends "base.html" %}
{% block contents %}
hello!
{% endblock %}
Django will silently throw away hello! because you wrote contents
instead
of content
. django-fastdev
will turn this into an error which lists the
invalid and valid block names in alphabetical order.
The standard error message for a bad reverse()/{% url %}
are rather sparse.
django-fastdev
improves them by listing valid patterns so you can easily see
the problem.
The error message for QuerySet.get()
is improved to give you the query
parameters that resulted in the exception.
A common mistake is to make a form clean method and make a spelling error. By
default Django just won't call the function. With django-fastdev
you will get
an error message telling you that your clean method doesn't match anything.
This is also very useful during refactoring. Renaming a field is a lot safer
as if you forget to rename the clean method django-fastdev
will tell you!
By default, django-fastdev
will check only forms that exist within your project,
and not third-party libraries. If you would like to enable stricter validation that will
extend to ALL forms, you can set this by configuring FASTDEV_STRICT_FORM_CHECKING
to True
in your Django settings.
The initial model checks can be quite slow on big projects. django-fastdev
will move these checks to a separate thread, so the runserver startup time is
lowered, so you don't have to wait for the runserver restart as long.
First install: pip install django-fastdev
In settings.py
add django_fastdev
to INSTALLED_APPS:
INSTALLED_APPS = [
# ...
'django_fastdev',
]
Enjoy a nicer Django experience!
BSD