ModelChoiceTitleField is a ModelChoiceField descendent that creates <OPTIONS> with title elements based on the field specified in title_source_field:
priority=ModelChoiceTitleField(Priority.objects.all(),
initial=Priority.objects.get(default=True).id,
title_source_field='long_description')
That will generate a `<SELECT>` element looking like:
<select name="priority" id="id_priority">
<option value="1" title="Some extremely important task.">Emergency</option>
...
</select>
In the `<option>` above, the title was retrieved from the `long_description` field for the instance of the Priority class. The word Emergency came from a call to the instance of the Priority class' `__unicode__()` method. The value of the option is the PK for the instance of the Priority class.
If you currently use `{% csrf_token %}`, you will notice it prints a hidden div, and an xHTML input tag. What if you don't want that hidden div, and/or you want your page to validate with HTML and not xHTML.
This snippet returns only the csrf token itself, and none of the related HTML code. You can use it like this.
`<input type="hidden" name="csrfmiddlewaretoken" value="{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}" >`
The middleware assigns a unique identifier for session. The session id doesn't depend of session or whatever else. It only need cookies to be turned on.
The session id is reassigned after client close a browser. Identifier of the session could be read from request: request.current_session_id.
You can setup name of the cookie in yours settings module (FLASH_SESSION_COOKIE_NAME).
request.current_session_id is lazy. It means the ID will be assigned and cookie will be returned to client after first usage.
Add it to MIDDLEWARE_CLASSES and you will get request.current_view getting values like "connection.views.index" or "django.views.static.serve" - one example usage is to show different help text based on current view name.
EDIT: actually, you can use process_view middleware function where you automatically get current_view as first parameter.
Simple logging decorator. Logs the function name along with the message.
`Jul 14 16:10:42 mtzion test_func: 1 two`
Define SYSLOG_FACILITY in settings.py.
import syslog
SYSLOG_FACILITY = syslog.LOG_LOCAL1
Quick and simple twitterize filter to turn Twitter usernames into profile links on your own sites.
Add the filter code to your own template tags (http://docs.djangoproject.com/en/dev/howto/custom-template-tags/).
A common problem (it hit the Django mailinglist a couple of times) is that if you get `models.Topping.objects.all()`, you get a list of toppings, although they stand for other classes such as `SalamiTopping` or `CheeseTopping`. If you need the actual object, just derive `Topping` from `PolymorphicModel`, and say `topping.actual_instance`. This will give you e.g. a `SalamiTopping`.
Sometimes you just want to check for the actual class. You can get it by saying `topping.content_type.model_class()`.
There is a slight performance impact when creating objects because they have to be saved twice.
NEWS: A good alternative to this approach is the [InheritanceManager](https://github.com/carljm/django-model-utils/blob/master/README.rst).
This is a copy of [snippet 654](http://djangosnippets.org/snippets/654/), modified to allow dynamic MEDIA_URL, as you might need that for SSL in combination with [snippet 1754](http://djangosnippets.org/snippets/1754/).
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 the current MEDIA_URL added by a context processor.
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:MEDIA_URL }}
This is a simple calendar widget in Django 1.2 for Jalali Calendar usages. It actually sends a Gregorian value to server, so if you need further manipulations, you can use [GNU Jalali Calendar](http://home.gna.org/jalali-calendar/). You should also have [JalaliJSCalendar](http://farhadi.ir/works/jalalijscalendar) set up in your project.
Also posted on: [Django Jalali Calendar Widget](http://texscribbles.blogspot.com/2010/06/django-jalali-calendar-widget.html)
The default Django urlize filter does not work with html nicely so here I've used an HTML parser [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) to quickly search through each text node and run the django urlize filter on it.
Optimizations could be made to include a regex in the soup.findAll() method's text argument to only search those text nodes which matched a url pattern. You could also modify the method to convert the text to urls, such as using your own custom url filter.
Makes manage.py testserver accept a `--noinput` argument to delete test database without asking if it already exists; makes it emit `django.core.management.commands.testserver.testserver_setup` signal after fixtures are loaded and before server itself is started to allow custom postprocessing.
A coverage test runner that uses the class-based runner introduced with Django 1.2.
Put it in your python path and add to your `settings.py`:
TEST_RUNNER = 'path_to.CoverageRunner'
COVERAGE_MODULES = [
'blog.views',
'projects.views',
'middleware',
]
Compatible with Django 1.2 and higher. You also need Ned Batchelder's `coverage.py` module (`pip install coverage`).
A shortcut for generating img-s with predefined classes and attributes that mimics a html tag, while resolving context variables inside {% %} without crutches like tag/stuff/endtag.
Used as `{% icon test class="spam" eggs="{{ object.pk }}" %}` yields `<img src="http://host.tld/media/icons/test.png" alt="" class="icon16 spam" eggs="42"/>`.
Not customizable here for simplicity.
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.