This should work as a `django.views.generic.list_detail` generic view but will produce PDF version of given template.
This code is merged code from perenzo's [example](http://www.djangosnippets.org/snippets/659/) and code from `django.views.generic.list_detail` module.
`pisa` package is required from (http://www.htmltopdf.org/download.html) with `html5lib` package and Reportlab Toolkit 2.1+
NOTE: this is code for Django 0.96. In Django 1.0 change in line 3: ObjectPaginator to Paginator
A simple way to add `date_created` and `date_modified` timestamps to a model. Adds a `date_created` timestamp when the object is first created and adds a `date_modified` timestamp whenever the item is saved.
**Note:** You might be tempted instead to use: `date_created=models.DateTimeField(default=datetime.now())` but that won't work as Python will calculate `datetime.now()` only once when it interprets your model. This means that every object created will get the same `date_created` timestamp until you restart your server.
This essentially wraps [snippet 917](http://www.djangosnippets.org/snippets/917/) (with full credit to author ncw) in a convenience function so that you can type:
admin_register(admin, namespace=globals())
or more concisely:
admin_register(admin, globals())
at the end of your admin.py file without having to register each model and admin class individually.
Works much like an inclusion_tag but allows the template_name to be given as an argument or defaults to partials/MODELNAME.html where MODELNAME is 'got' from the context object you want to render. Its very rough so improvements very welcome!
It would be nice to be able to pass new context variables as template tag [keyword] arguments for use in the template to be rendered so you basically have a template tag equivalent for render_to_string...
Example usage in a template:
{% render_partial post %} or {% render_partial post partials/super_post.html %}
When you switch you django project from 0.9.6 to 1.0, you can use this script to generate admin.py automatically.
You need copy cvt.py to the parent directory of your project(where your project lies) and type "python cvt.py <project> <app>". The admin.py will generated in the <project>/<app>(where it should be!).
Enjoy this small work!
This TestSettingsManager class takes some of the pain out of making temporary changes to settings for the purposes of a unittest or doctest. It will keep track of the original settings and let you easily revert them back when you're done.
It also handles re-syncing the DB if you modify INSTALLED_APPS, which is especially handy if you have some test-only models in tests/models.py. This makes it easy to dynamically get those models synced to the DB before running your tests.
Sample doctest usage, for testing an app called "app_under_test," that has a tests/ sub-module containing a urls.py for testing URLs, a models.py with some testing models, and a templates/ directory with test templates:
>>> from test_utils import TestManager; mgr = TestManager()
>>> import os
>>> mgr.set(INSTALLED_APPS=('django.contrib.contenttypes',
... 'django.contrib.sessions',
... 'django.contrib.auth',
... 'app_under_test',
... 'app_under_test.tests'),
... ROOT_URLCONF='app_under_test.tests.urls',
... TEMPLATE_DIRS=(os.path.join(os.path.dirname(__file__),
... 'templates'),))
...do your doctests...
>>> mgr.revert()
Here's an example of writing generic views in an object-oriented style, which allows for very fine-grained customization via subclassing. The snippet includes generic create and update views which are backwards compatible with Django's versions.
To use one of these generic views, it should be wrapped in a function that creates a new instance of the view object and calls it:
def create_object(request, *args, **kwargs):
return CreateObjectView()(request, *args, **kwargs)
If an instance of one of these views is placed directly in the URLconf without such a wrapper, it will not be thread-safe.
This replaces the html select box for a foreign key field with a link to that object's own admin page. The foreign key field (obviously) is readonly.
This is shamelessly based upon the [Readonly admin fields](http://www.djangosnippets.org/snippets/937/) snippet. However, that snippet didn't work for me with ForeignKey fields.
from foo.bar import ModelLinkAdminFields
class MyModelAdmin(ModelLinkAdminFields, admin.ModelAdmin):
modellink = ('field1', 'field2',)
See the description in the blog entry at [http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/](http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/)