If you try to load a template named filename.LANG.html it will try to load filename.de.html first, then filename.html afterwards (assuming that German is the currently selected language).
Usage: Put in a file named langtemplateloader.py under your project, and replace django's default filesystem loader with this in the TEMPLATE_LOADERS section of settings.py:
TEMPLATE_LOADERS = (
'myproject.langtemplateloader.load_template_source',
# 'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
'django.template.loaders.eggs.load_template_source',
)
i use this to get the pk of a record before creation,
in my scenario to name an uploaded image:
def UPLOADTO( i,n ):
if not i.id:
id = get_nextautoincrement( i.__class__ )
else:
id = i.id
return str(id)+'.jpg'
This is an example how to dynamically import modules other than `models.py` from installed apps. It allows you to define the full module path just once in `INSTALLED_APPS` in the settings.
Using this technique makes it possible to write extensible and reusable apps as mentioned in [Abstract Models and Dynamicly Assigned Foreign Keys](http://djangotricks.blogspot.com/2009/02/abstract-models-and-dynamicly-assigned.html).
Probably, it would be great to have some helpers for doing this in django itself. For example:
from django.db import models
from django.utils import importlib
def import_installed(path):
"""
Imports a module from an installed app
>>> import_installed("myapp.forms")
<module 'myproject.apps.myapp.forms'>
"""
app_name, module = path.split(".", 1)
app = models.get_app(app_name)
return importlib.import_module(app.__name__[:-6] + module)
UPDATE: Reported as ticket [#10703](http://code.djangoproject.com/ticket/10703)
This snippet display a human readable date diff. You give it the your date in parameter and the diff with datetime.datetime.now() is returned. The diff must be positive to be more accurate (future dates are not supported)
Usage:
{{ status.created_at|date_diff }}
Will give something like:
less than 1 minute ago
13 minutes ago
1 hour ago
etc.
Based on [Fuzzy Date Diff Template Filter](http://www.djangosnippets.org/snippets/1347/)
Problem
=======
The FormPreview class provided by contrib.formtools helps automate a common workflow. You display a form, then force a preview, then finally allow a submit. If the form gets tampered with, the original form gets redisplayed.
Unfortunately, this class can only be used when you have an html form that is backed by exactly one Django form. No formsets, no html forms backed by more than one Django form.
Solution
========
I was asked to create exactly this sort of workflow for a highly complex form + formset. The ComplexFormPreview class provides a base class to help with this problem. As with FormPreview, you must override a few functions.
Code
====
The abstract ComplexFormPreview class can live anywhere on your python path. Import it and subclass is exactly like you would contrib.formtools FormPreview.
The self.state dictionary is passed to all response calls as the context for your templates. Add any objects you need in your template to this dictionary. This includes all forms, formsets, and any additional variables you want in your template context.
Override the parse_params if you need to get any args/kwargs from your url. Save these values in self.state if you want them in your template context.
Override the init_forms method to do setup for all of your forms and formsets. Save all your forms in self.state. You should provide a unique prefix for all forms and formsets on the page to avoid id collisions in html.
*VERY IMPORTANT NOTE*: init_forms is called with a kwargs dictionary. You need to pass **kwargs to all of your form definations in init_forms. This is how the POST data is going to be passed to your forms and formsets.
*VERY IMPORTANT NOTE No. 2*: all of the validation is handled inside the class - all forms will be found and validated, and we will only proceed when everything is found to be valid. This means that you can use the class as a view directly, or provide a thin wrapper function around it if you want.
Override the done method to handle what should be done once your user has successfully previewed and submitted the form. Usually, this will involve calling one or more save() calls to your various forms and formsets.
Because you now have multiple forms, the default contrib.formtools templates don't work. You must make custom templates that reference all of your various forms. The stage_field, hash_field, and hash_value fields are used exactly like the formtools examples. Follow the basic layout demonstrated in the example templates, and substitute your custom forms for the default form.
Example views.py
================
The views.py demonstrated here has many hooks into my project, including using some [complex formset classes](http://www.djangosnippets.org/snippets/1290/). It won't work for you without being customized, but it will demonstrate how to override the default ComplexFormPreview.
Based on our [first version of soaplib service integration](http://www.djangosnippets.org/snippets/979/), this second one adds Basic Auth with credentials specified in settings.
It can be tested with [django soaplib test cliente](http://www.djangosnippets.org/snippets/1406/)
This code *monkey patches* soaplib client to allow the usage of django test client for local web service testing (without a running server). It adds *basic* authentication.
If you app defines some URLs with a name, and you want to overwrite this
at project level with a different view you can use this snippet. You only need
to change on line in the application code (the import statement).
Permit to redirect desired domain name to the 'domain' of Site app.
Useful if you have different domains name for the same website.
#1. Add to your settings DOMAINS_ALIAS like this:
DOMAINS_ALIAS = (
'my-second-domain.com',
'www.my-second-domain.com',
'third-domain.com',
'www.third-domain.com',
)
notice: all these domains are redirected to the **domain** db entry of Site ID.
#2. add all these domains to ServerAlias directive in your vhost apache configuration.
#3. enable the middleware by adding to your MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
...
'utils.middleware.domainsalias.DomainsAliasMiddleware',
...
)
`dumpdata` without `MemoryErrors`, with progress notification. Most of the real work is done by snippets [1400](http://www.djangosnippets.org/snippets/1400/) and [1401](http://www.djangosnippets.org/snippets/1401/).
./manage.py dumpdata_stream --format=xml > big_dump.xml
This is basically the stock Django `dumpdata` with a few modifications. Django devs: it's hard to reuse parts of most Django management commands. A little refactoring could go a long way.