- Provide middleware;
- If user is anonymous, look for file in BUILD_DIR
- If it exists, return it immediately
- If it doesn't exist, call the view
- If it doesn't exist and it returns a 2xx, write it to the builder
- If it doesn't exist and it returns a 3xx, write the 30x.html template to the builder, pointing at the redirect endpoint
- If it doesn't exist and it returns a 404, write the 404.html to the builder
- If the user is not anonymous, render the original view, including other middlewares etc.
- Middleware has an inclusions regex -- see django-moreloaders for regex ideas.
- If user is anonymous, look for file in BUILD_DIR
- Provide builders;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
if 'HTTP_X_REQUESTED_WITH' in environ: | |
result = environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 2.7133121490478516 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import QueryDict | |
new_querystring = QueryDict('a=1&b=2', mutable=True) | |
new_querystring.update({'c': 3, 'a': 4}) | |
new_querystring.urlencode() # outputs 'a=1&a=4&c=3&b=2' | |
new_querystring['a'] = 5 | |
new_querystring.urlencode() # outputs 'a=5&c=3&b=2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I have a known type ... it is X. I should use lazy() then ... | |
# but SimpleLazyObject "just works", while lazy is obstinate. | |
from django.utils.functional import lazy, SimpleLazyObject | |
from collections import namedtuple | |
X = namedtuple('X', ['a', 'b']) | |
y = lazy(lambda: X(a=1, b=2), X) | |
z = SimpleLazyObject(lambda: X(a=1, b=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
$ python manage.py statictemplate > a_static_dir/error_hook.js | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
from django.template.defaultfilters import stringfilter | |
register = template.Library() | |
@register.filter | |
@stringfilter | |
def pathcss(val): | |
parts = list(val.strip('/').split('/')) | |
results = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from django.utils.functional import lazy | |
>>> lazy(lambda: None, None)() | |
Traceback (most recent call last): | |
File "<console>", line 1, in <module> | |
File "django/utils/functional.py", line 181, in __wrapper__ | |
return __proxy__(args, kw) | |
File "django/utils/functional.py", line 83, in __init__ | |
self.__prepare_class__() | |
File "django/utils/functional.py", line 95, in __prepare_class__ | |
for type_ in reversed(resultclass.mro()): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def runme(): | |
return 1 | |
def dontrunme(): | |
return 2 | |
a = { | |
'key1': runme, | |
'key2': runme, | |
'key3': dontrunme, |
- If you type
MyModel.<manager>.filter
anywhere, you're doing it wrong.- Either create a
Manager
and do it there, or, better yet, usedjango-model-utils
'sPassThroughManager
and write a proper, chainable queryset. - Possibly better than that would just be to have a
queries
module, which contains functions that return the correct querysets. Seriously, tracking down queries being made is much easier if there's only one place to look.
- Either create a
- If you're doing
MyInstance.<related_name>.filter
, I still hate you. Fix your manager/queryset or do thatqueries
module I mentioned. - If you want to do anything to a field on a Form, do it via
self.fields[<fieldname>]
in the__init__
method. Everything else is crap. - If you're using a context processor and it touches the database, you'd better make it
lazy()
or I'll mow you down.- Seriously, otherwise it'll do queries for every
RequestContext
using view, even if it doesn't get used.
- Seriously, otherwise it'll do queries for every
- Use
django-pdb
,django-debug-toolbar
and `django-devs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyForm(ModelForm): | |
def __init__(self, *args, **kwargs): | |
super(MyForm, self).__init__(*args, **kwargs) | |
self.fields['myfield'].label = 'hello' | |
class Meta: | |
model = MyModel |