This will return a template-friendly list of dictionaries when using custom SQL. The dictionary can be accessed just as a normal model/queryset.
Example of use (in view):
qkeys = ['artist','album']
query = """
SELECT "artist","album" FROM "music"
"""
new_list = csql(request,query,qkeys)
(in template)
{% for row in new_list %}
{{ row.artist }} {{ row.album }}
{% endfor %}
This is to be used as a template filter. See [django documentation](http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-filters) for adding custom filters.
Example of use:
name = "bart simpson"
`{{ name|cap }}` will convert 'name' to "Bart Simpson"
It works on space-separated names, as well as the following:
* converts "mcfly" to "McFly";
* converts "o'neill" to "O'Neill";
* converts "barker-bradley" to "Barker-Bradley"
Originally posted by [akaihola](http://www.djangosnippets.org/users/akaihola/) as [snippet #169](http://www.djangosnippets.org/snippets/169/). I just redid it as a filter.
This is some very simple middleware that keeps track of the last 3 succesful requests for each visitor. This can be useful if you want to redirect the visitor to a previous path without relying on a hidden field in a form, or if you simply want to check if a visitor has recently visited a certain path.
Note that this relies on the session framework and visitors actually accepting cookies.
This can be easily modified to hold more requests if you have a need for it.
Simple template filter to encode a variable to JSON format
Usage:
{% load json_filters %}
{% block content %}
<script type="text/javascript"><![CDATA[
var items = {{ items|jsonify }};
]]></script>
{% endblock %}
I'm using JsonResponse for the views but I also want to have preloaded JSON data into the page output
Renders an select field with some optgroups. Some options can be outside the optgroup(s).
The options and labels should be in a tuple with ((label, choices),) where choices is a tuple ((key, value), (key2, value2)). If a label is null or blank, the options will not belong to an opt group.
Useful for when you want to use an instance's values as the initial values of a form which you didn't use `form_for_instance` to create.
Handles foreign keys and many-to-many fields just fine.
A slightly improved version of [snippet #195](/snippets/195/) which keeps the logic but makes use of the `simple_tag` decorator to drastically simplify the code.
For an alternative to this sort of tag, check out the media context processor in my [template_utils app](http://code.google.com/p/django-template-utils/).
Sometimes it's nice to know if your visitors came from a search engine such as Google or Yahoo. I use this as a component in determining the popularity of blog articles for search keywords, so I know which articles to automatically feature or suggest. Maybe you'd like to use it to highlight the visitor's search terms on the page.
This isn't actually middleware in the sense that it defines any of the middleware interfaces. It's intended to be the base for a middleware method that you write, depending on what you want to happen when you detect that the visitor came from a search engine. In the example `process_view` method, I detect when the request is going to use the `object_detail` view and log the search query for that object in the database.
This a small but very handy view that gives you a convenient direct access to your templates.
Now suppose you saved the snippet under `misc.py`, it's critical to add this snippet (or a similar one, once you get the idea) to your `urls.py`:
if settings.DEBUG:
# Direct Templates
urlpatterns += patterns('misc',
(r'^template/(?P<path>.*)$', 'direct_to_template', {'template': '%(path)s'}),
)
Now you are able to access any of your templates, in different directories by specifying their path after `template/`. e.g., http://example.com/template/news/index.html
Of course you can change it as you want, you can also add other values to the dict argument, the only required key is `'template'`. The whole dict is made available in the template as a context.
All GET parameters are made available in the template too. So `http://example.com/template/news/index.html?title=Testing Title` will make the `{{ title }}` var available in your template. So you can substitute basic variables quickly.
This is was inspired by [django.views.generic.simple.direct_to_template](http://www.djangoproject.com/documentation/generic_views/#django-views-generic-simple-direct-to-template)
Returns an absolute URL pointing to the given media file.
The first argument is the path to the file starting from MEDIA_ROOT.
If the file doesn't exist, empty string '' is returned.
For example if you have the following in your settings:
MEDIA_URL = 'http://media.example.com'
then in your template you can get the URL for css/mystyle.css like this:
{% media 'css/mystyle.css' %}
This URL will be returned: http://media.example.com/css/style.css.
Super stripped down filter to truncate after a certain number of letters. Ex: {{ long_blurb|truncchar:20 }} will display 20 characters of the long blurb followed by "..."
Taken from the [longer description on my blog][1]:
> One of the great things about Django is its simple and flexible URL handling. If your Django work, like mine, includes converting existing sites, you’ll probably be doing some URL cleanup along the way...
Just plug your old/new URL pairs into `redirects`.
[1]: http://e-scribe.com/news/290