Snippet List
Serves images from a local directory, but if it doesn't find it locally, then go look for it on a server. This is useful for sites with very large amounts of static content.
Instead of copying all your prod images to dev every time you update the database, simply use this in your URL patterns to serve the images:
`(r'^(?P<path>.*)$', 'static_fallback.serve', {'document_root' : '/path/to/my/files/'})`
By default, it caches the images locally in the path it was supposed to find them. The fallback server URL can be specified in urls.py or settings as FALLBACK_STATIC_URL.
Thanks to Johnny Dobbins for the idea. Based on the Nginx pass through image proxy concept.
For more info [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)
- image
- static
- images
- fallback
Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage:
set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py')
More more information, see [Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage:
set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py')
More more information, see [http://menendez.com/blog/maintain-contants-through-south-data-migration/](http://menendez.com/blog/maintain-contants-through-south-data-migration/).
Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc.
This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.
You need the mini-detector middleware installed [http://www.iterasi.net/openviewer.aspx?sqrlitid=-e2dfig9w0yrclxaigp-uw](http://www.iterasi.net/openviewer.aspx?sqrlitid=-e2dfig9w0yrclxaigp-uw).
This is a drop in replacement to render_to_response. When using mini_render_to_response it will try to load a version of your template with mini at the end. For example "home_mini.html" instead of "home.html". If it doesn't find the _mini version it falls back to the regular "home.html" version of your template.
Easy way to maintain a "small screen" version of your templates for iPhone or other small screen devices.
- templates
- render
- iphone
- mini
Include in your code like this:
t=Timer()
Then use it like this:
t.tick('Some optional description')
It will output the time spent between the tick and the previous tick (or inception) and the total time spent since it began tracking time. Can be placed multiple times in a long segment of code. Can be used to break out the amount of time being spent on various parts of your code so you can focus on optimizing those sections.
- time
- high-performance
- profiling
- timer
- optimize
Replaces something like this:
cache_key = 'game1'
the_game = cache.get(cache_key)
if not the_game:
the_game = Game.objects.get(id=1)
cache.set(cache_key, the_game, 60*24*5)
With this:
the_game = get_cache_or_query('game1', Game, seconds_to_cache=60*24*5, id=1)
- cache
- optimization
- get_cache_or_object
This class will automatically create a django choices tuple like this:
STATUS_CHOICES = django_choices(Draft=1, Public=2, Closed=3)
Additionally, it includes a method that converts the choices tuple to a dictionary. Like this:
STATUS = STATUS_CHOICES.to_dict()
Those types can come in handy when you need to use those magic values in your code. Best done within the model once so everyone can use it. Code based on: http://www.djangosnippets.org/snippets/455/.
By the way, if you want to just have the method without having to convert to the newer syntax.. it's backward compatible. Just add django_choices in front of the first paren for your choices tuple.
menendez has posted 8 snippets.