This snippet proposes a solution for pre-exisiting database tables that have no counterpart in your django application models.py module.
My use case was a comercial systems where pricing tables were created on-the-fly as
a result of a stored procedure's execution inside the database. These pricing tables had
no counterpart class model. Therefore, to access these tables from Python code you had to
bypass Django's ORM and use plain-old SQL through cursos.execute().
This snippet's strategy is to create a (model) class on the fly and inject that class
inside models.py namespace. After that you can use the generated class and Django's ORM
machinery to access the tables, as if they were created by syncdb.
Cute little dictionary-like object that stores keys as regexs and looks up items using regex matches. It actually came in handy for a project where keys were regexes on URLs.
I tend to have all my python code together in a 'python' directory.
e.g. a typical project layout of mine looks like:
/python
/python/myproject - project python code
/python/django - local copy of django
/python/foolib - some third party library
/media
/templates
...
Since I don't want to set the python path explicitly I just assume the 'manage.py' is in the right place and use its __file__ variable to set up the python path correctly.
I use the same trick for my settings.py for setting MEDIA_ROOT and TEMPLATE_DIRS.
This snippet provides a form_for_model and form_for_instance function which create newforms that respect the unique and unique_together constraints defined in the model.
One thing about the coding style in this code snippet: Classes are capitalized even if they're passed as arguments. Thus "Model" is a model class while "model" is a model instance.
There are a lot of reasons why you should not do this.
But nevertheless it might be useful in certain situations.
If your database does not change and you want to use validators etc you better use inspectdb from django-admin.py
First off: This snippet is more or less obsoleted by [snippet #259](/snippets/259/). The way that snippet #259 uses feels cleaner and more usable. It is basically the same code but the magic works a little bit different. In case you prefer this way, I've left this snippet as-is.
Maybe you know this problem: you have some objects in your database and would like to display them in some way. The problem: the Django ORM does not provide any way to sort model instances by a user-defined criterion. There was already a plug-in in SQLAlchemy called [orderinglist](http://www.sqlalchemy.org/docs/plugins.html#plugins_orderinglist), so implementing this in Django was basically not a big deal.
Usage is (due to use of meta-classes) quite simple. It is recommended to save this snippet into a separate file called `positional.py`. To use it, you only have to import `PositionalSortMixIn` from the `positional` module and inherit from it in your own, custom model (but *before* you inherit from `models.Model`, the order counts).
You may want to access to "global" errors in your template, typically when you have a custom `clean()` method which checks several fields. `{{ form.errors }}` gives an < ul >, which is often not what you want, and `{{ form.errors|join:", " }}` iterates in the keys of `errors` (which are the names of fields, uninteresting).
The global `errors`'s key is "__all__", and Django doesn't allow us to do `{{ form.errors.__all__ }}`.
This method returns an array, like a classic `{{ form.field.errors }}`, and can be used with a join : `{{ form.get_global_errors|join:", " }}`.
1) You've ported an existing web site to Django.
2) The new site URLs don't have html (or htm) extensions.
3) The old site had URLs with html extensions.
4) You want existing bookmarks to work.
Use this middleware to removes trailing .htm and .html extensions from incoming URLs (GETs only) so that your new site honors existing bookmarks. Locate it in settings.MIDDLEWARE_CLASSES near CommonMiddleware because it has similar middleware stack location requirements. If an incoming URL has an html extension, ZapDotHtmlMiddleware strips it out and redirects.
Add a value to the context for easy access and for access from include templates.
NOTE: This tag is composed from Django ticket:
http://code.djangoproject.com/ticket/1322
** Django Proxied Behind Apache Path**
Middleware and Context Processor for django applications behind a proxy like Apache's mod_proxy. Setting an HTTP header with the base path of the django application in the Apache mod_proxy configuration, we can pull that variable out of the request and adjust our template URIs and redirects. This allows us to serve the same Django application out from behind multiple VirtualHosts at different URL paths.
Example use in templates:
<a href="{{ base_path }}/login/">Login</a>
Example Apache configuration:
<LocationMatch ^/path/to/django/app/.*>
RequestHeader set X-Base-Path "/path/to/django/app"
</LocationMatch>
RewriteRule ^/path/to/django/app/(.*) http://django-host:8080/$1 [P,L]
In settings.py:
VHOST_HEADER = "HTTP_X_BASE_PATH"
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 "..."
This is quite a hack (need to modify Django code), but relatively simple and stable. It displays various times in whichever increments and columns you specify, rather than just Midnight, Noon, Now & 6am. You can use it throughout your admin and newforms-admin code.
This is a (slightly updated) copy of the ticket #1848 which wasn't included in trunk. http://code.djangoproject.com/ticket/1848
Middleware for stripping all html comments from the response content before returning it to the client.
This will also strip inline javascript with htmlcomments put around it!
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.