Provides python-like string interpolation.
It supports value interpolation either by keys of a dictionary or
by index of an array.
Examples:
interpolate("Hello %s.", ["World"]) == "Hello World."
interpolate("Hello %(name)s.", {name: "World"}) == "Hello World."
interpolate("Hello %%.", {name: "World"}) == "Hello %."
This version doesn't do any type checks and doesn't provide
formating support.
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.
This is in the spirit of php's include_once or a C preprocessor #ifndef. Kind of. As you might've guessed, the output of
{% renderonce %}foo{% endrenderonce %}
{% renderonce %}foo{% endrenderonce %}
in a template will be a single 'foo'. I use it for js script tags personally, to prevent duplicate inclusions. If you ended up here, chances are you've already explored the "use inheritance" or "use django-(app X)" solutions, so feel free to omit such comments.
This is a minimal template loader for Django 1.2 or higher that loads [Jinja2](http://jinja.pocoo.org/2/) templates. It is better integrated with Django than using Jinja2 directly:
* Your view code is the same
* Unmodified generic views use it
* RequestContext and context processors still work
To use it, add the following to you `settings.py` file:
TEMPLATE_LOADERS = (
'jinja2_for_django.Loader',
)
It searches for templates in the same places as `django.template.loaders.app_directories.Loader` − that is in the `templates` directory of each installed app.
Django custom and default template tags and filters are not available. Some are the same in Jinja2, but you need to replace the others yourself. You can add global filters and variables (such as functions) in the `Loader.env.filters` and `Loader.env.globals` dicts. You can not add tags. See the [Jinja2 documentation](http://jinja.pocoo.org/2/documentation/) for more details.
This is a Model base class used to support internationalization (i18n) for your models.
This code extends the Django's Model class so you can use all available options from Model safely. Basicly, it uses introspection to create a sub-model to your model to hold translation.
**Features:**
1. Simplicity of use. You simply extend your model with this class and add all the fields that needs to be translated are placed under the `locale` sub-class;
2. The code uses the `django.utils.translation.get_language()` to select the current language;
3. You can use `python ./manage.py syncdb` command safely;
4. Force the user to enter a translation for each language even if the fields can be blank. This makes sure that all objects are returned safely.
**Ordering by locale fields:**
To sort on translated fields, use the form of "model_i18n__transfieldname" (see code for example).
**Limitation:**
Do not use localized fields in __unicode__, the admin will throw an exception when you'll add a new item and do "save and continue".
Just drop a comment if you need more information.
(last update: 06/15/2010)
It's quite common to use Django's `static.serve` functionality to serve media assets via the built-in development server. However, I always find that this is far too noisy - every asset produces a line of output on the console, and any debug messages I want to put there are hard to see.
This management command monkey-patches the built-in `runserver` command so that it only generates a line of output for actual Django views - anything else is served as usual, but is not logged to the console. In fact the original version was already doing this for admin media, but not for your own media - I've just extended the logic.
Put this in the management/commands directory of an installed app, saving it as (for example) `runserver_quiet`, then just do `./manage.py runserver_quiet` to run - it will accept all the same arguments as the built-in version.
Allow you to specify a "General case formset/modelformset" and then alter the attributes of that formset, specificly: extra, can_order, can_delete and max_num.
So you specify:
>>> formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O'))
and then you want to dynamically add multiple fields with javascript and save the new ones. By default a formset only has 1 extra field. With this you can return a new formset (using the same queryset, forms, formset base class, etc) but with different attributes. So you could then add 10 extra fields if the user added 10 new forms.
Middleware class that checks the user agent against a known list of strings found in mobile devices, and if matched it then tries to determine the name of the template being rendered based on the convention of having every view use a keyword arg called "template". It then adds "mobile" to the template name and if the mobile template exists, it will override the "template" arg for the view with the mobile template name.
**I've since made a better snippet for this: [#2995](http://djangosnippets.org/snippets/2995/)**
based on [#1697](http://djangosnippets.org/snippets/1697/)
This one is even more generic since you can specify which fields to include or exclude, a custom description text for the drop-down menu and whether to output the header row.
### Problem: you want to limit posts to a view
This can be accomplished with a view decorator that stores hits by IP in memcached, incrementing the cached value and returning 403's when the cached value exceeds a certain threshold for a given IP.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.