Standarization of image fields (for being used when saving models). Automatically creates thumbnail. Change image name to /path/to/images/<my_field>-<id>.<ext>. Resize image and thumbnail to specified size (optionally can crop image to force size).
A replacement test runner which outputs a coverage report after the tests. Simply change your ``TEST_RUNNER`` setting to point to ``run_tests_with_coverage`` and you're good to go.
Note that 'as-is' this snippet reports the coverage of all modules underneath the app, by walking the directory tree and loading all of the .py modules (this may be a naive approach).
If you change it to use `get_coverage_modules()` instead, it will only display the coverage of modules that have been imported by the test suite, using the Python `inspect` lib, which may be more reliable.
Uses [coverage.py](http://nedbatchelder.com/code/modules/coverage.html). Based on ideas from: [1](http://www.thoughtspark.org/node/6), [2](http://blogs.23.nu/c0re/stories/15428/) and [3](http://siddhi.blogspot.com/2007/04/code-coverage-for-your-django-code.html)
This was born as a result of the fact that session data is shared across logins on a single browser. If you login as user1 and session data is stored, then login as user2 the same session data will be available to your application. Please see the ticket who's validity is at this point in question. Some feel that this is normal behavior.
http://code.djangoproject.com/ticket/6941
I use this code in conjunction with
http://code.google.com/p/django-registration/
Place this code in registration.__init__ and change registration.urls to have login and logout route to the new alternate versions alt_login, alt_logout.
I have only been using Python and Django for a couple months now so I hope that this implementation is not too terrible. It works for me. Enjoy.
This is a template filter to enable the use of the MEDIA_URL setting in content from the flatpages database table. It searches for {{ MEDIA_URL }} and replaces it with that found in your settings.
Note: To set up, drop the above code into a file called `media_url.py` in your templatetags directory in one of your INSTALLED_APPS, and add the filter to your flatpages template like so:
{% load media_url %}
{{ flatpage.content|media_url }}
[Original and further information available from here.](http://www.undefinedfire.com/articles/recursion-in-django-templates/)
**v1.1 Update (20/04/08):** Added the ability to recurse single elements as well as automatic skipping of empty elements.
Most of the tags are self explanatory, the only one that may cause confusion is the main `{% recurse %}` one. The format for this tag is `{% recurse [children] with [parent] as [child] %}` where “[children]” is the property that contains the children of the current element, “[parent]” is your starting element and “[child]” is the variable named used in the loop.
Example usage:
{% load recurse %}
... Headers and stuff ...
{% recurse category.category_set.all with categories as category %}
<ul>
{% loop %}
<li>
<h{{ level }}>{{ category.title }}</h{{ level }}>
{% child %}
</li>
{% endloop %}
</ul>
{% endrecurse %}
... The rest of the page ...
Tired of scrolling through hundreds of lines of code where the indentation is maddening?
Here's a middleware class that prettifys your html markup so it's nice and consistently indented. Intended only for debugging, and I add it to the middleware stack conditionally on TEMPLATE_DEBUG. Requires BeautifulSoup.
It's a pain to import all the Django models you want to use in the Python shell every time you start it. Here's how you can get IPython to autoload all your Django models for you every time you start the shell using ./manage.py shell.
Put the code in a .py file in the root of your project. Then tell IPython to load the script in ~/.ipython/ipythonrc in the "Python files to load and execute" section.
If you're like me, you've got a models with a lot of fields/foreignkeys and often only want to edit a portion of the model in a form. Add this method to your custom form class and use it in place of the save() method.
This snippet allows you to use YUI's autocomplete widget in a easy way.
1. Download YUI (http://developer.yahoo.com/yui/) library and put it into MEDIA folder (in my case I put YUI/build/ directory as base/yui/ in my MEDIA folder)
2. Create lookup view for your autocomplete field.
See 'test_ajax_ac' function to see how this lookup view may be built. You have to define JsonResponse somewhere in your files. JsonResponse is taken from: http://www.djangosnippets.org/snippets/154/
3. Define url for newly created view in urls.py (in usual way)
4. Include necessary .js and .css files in your page (see example in test_ajax.html)
5. Assign widget to a field - see form's __init__ at UserForm in the example.
Additional (optional) parameters are: format_result_fname (name of javascript function for formatting results - see YUI docs for examples)), item_select_handler_fname (name of javascript function for handling item select event (see YUI docs)).
When using YUI take care about proper skin - you'll possibly need to define wrapper like:
`<div class="yui-skin-sam">....</div>`
around your html code.
This handler is useful if you serve static/media files directly from Apache only to authenticated users. It checks if a user is not anonymous (and therefore logged in) and redirects to the login site if needed.
The following apache config activates the handler:
<Location "/site_media/company1">
#SetHandler None ##Uncomment if you serve static files in the same virtual host
PythonOption DJANGO_SETTINGS_MODULE mysite.settings
PythonAuthenHandler mysite.myhandler
PythonPath "['/path/to/project'] + sys.path"
Require valid-user
</Location>
This allows for urls in the form of `/grandparent-slug/parent-slug/self-slug/` where the number of parent slugs could be 0 to many.
You'll need to make sure that it is your last urlpattern because it is basically a catch-all that would supersede any other urlpatterns.
Assumes your page model has these two fields:
* `slug = models.SlugField(prepopulate_from=("title",), unique=True)`
* `parent = models.ForeignKey("self", blank=True, null=True)`
In your settings file:
TEMPLATE_TAGS = (
"djutils.templatetags.sqldebug",
)
Make sure load_templatetags() gets called somewhere, for example in your apps __init__.py
*Edit: Updated to work with templatetag libraries that use certain critical django-bits.*
UUIDField is a field which stores an uuid generated by pythons new uuid module.
it's included with the python 2.5 distribution by default, but if you run an older version of python you can happily copy the file from 2.5 to django/utils/uuid.py or your project directory.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.