This will use the syslog system to log your logs. This is usefull when you use WSGI and want to have a per day logging file (TimedRotatingFileHandler is not process safe, and you may lose data when using it with WSGI).
Under a debian system, you'll have to modify **/etc/rsyslog.conf** and add:
local0.* -/var/log/django/django.log
local1.* -/var/log/django/payment.log
Place the above code in your view, and then you can use it to specify what template to use. Set elements of the context as a dictionary and that gets passed to the template as well. For example:
In view.py:
####################################
@with_template('friends/index.html')
def friends(request, context, username):
context['user'] = User.objects.get(username = username)
in friends/index.html:
{% extends "base.html" %}
{% block content %}
<h1>{{ user.username }}'s Friends</h1>
This is a Django Template Tag to protect the E-mail address you publish on your website against bots or spiders that index or harvest E-mail. It uses a substitution cipher with a different key for every page load.
It basically produce a equivalent Javascript code for an email address. This code when executed by browser make it mailto link.
**Usage**
`{% encrypt_email user.email %}`
**More Info **
[Template Tag to protect the E-mail address](http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/)
Template to make a galleriffic gallery for django photologue.
Uses the settings from this demo
http://www.twospy.com/galleriffic/example-2.html
This is now the default template for the django-cms photologue plugin
http://www.django-cms.org/en/extensions/cmsplugin-photologue/detail/
This Snippet allows for your project's apps names to be displayed as you want in the Admin, including translations.
The lists of apps and languages are created from your settings.py file.
**How to use**
1st part:
- Create a application called 'apps_verbose' in you project with the models.py and admin.py showed here
- Create a folder inside it with named 'templatetags' with the verbose_tags.py file inside it.
- Add this app to installed apps in your settings.py
- If you change this app name, dont forget to correct the imports.
2nd part:
- Create a folder named 'admin' in your templates folder and copy the following files form your /django/contrib/admin/templates/admin/ folder.
- /app_index.html
- /base.html
- /change_form.html
- /change_list.html
- /delete_confirmation.html
- /delete_selected_confirmation.html
- /index.html
- /object_history.html
- Make the necessary changes in each file, like shown here.
3rd part:
- Create translations in the Admin and enjoy.
Runs model methods on save, create, update, delete
Similar to Rails hooks
**Usage:**
*in models.py*
from myproject.hooks import connect_hooks
class MyModel(models.Model):
#...
# only on first save of a newly created object
def before_create(self): print self
def after_create(self): print self
# not on first save of a newly created object
def before_update(self): print self
def after_update(self): print self
# any save, new object or update
def before_save(self): print self
def after_save(self): print self
# delete, self is still available after delete
def before_delete(self): print self
def after_delete(self): print self
**connect_hooks(MyModel)**
This is the same as [django.forms.extras.widgets.SelectDateWidget](http://code.djangoproject.com/browser/django/trunk/django/forms/extras/widgets.py#L16) but changing the order of the rendered select boxes to: day, month, year.
A filter to integrate Google Closure compiler in django-compress plugin.
1. [download django-compress](http://code.google.com/p/django-compress/)
2. install it
3. [download Closure Compiler](http://code.google.com/closure/compiler)
4. put the jar at the root of your project
5. put this snippet as a **__init__.py** file in a **google_closure** directory in the filters directory of the plugin
6. add `COMPRESS_JS_FILTERS = ('compress.filters.google_closure.GoogleClosureCompilerFilter',)` to your settings.py
You can test `COMPRESS_CLOSURE_JS_ARGUMENTS = {'compilation_level': 'ADVANCED_OPTIMIZATIONS', }` in your settings.py too
If you are using django-mptt to manage content (eg heirarchical categories) then it needs a bit of help to make a nice admin interface. For many-to-many fields, Django provides the quite nice FilteredSelectMultiple widget (a two-pane selection list with search box) but it only renders 'flat' lists... if you have a big category tree it's going to be confusing to know what belongs to what. Also, list items are sorted alphabetically in the js, which won't be what you want.
This snippet extends FilteredSelectMultiple to show the tree structure in the list.
You'll also need the js from this snippet: [#1780](http://www.djangosnippets.org/snippets/1780/)
For usage details see my blog at:
[http://anentropic.wordpress.com](http://anentropic.wordpress.com/2009/11/05/more-django-mptt-goodness-filteredselectmultiple-m2m-widget/)
This builds on a couple of other people's hacks to effectively manage django-mptt models via the admin.
One problem you find is if you use the actions drop-down menu to ‘delete selected’ items from your mptt model… the bulk actions bypass the model’s delete method so your left/right values for the tree aren’t updated.
What you need is a way to automatically rebuild the tree after a bulk action.
This code provides that facility.
Full details on my blog:
[http://anentropic.wordpress.com/2009/10/26/](http://anentropic.wordpress.com/2009/10/26/making-admin-bulk-delete-action-work-with-django-mptt/)
This is an improvement on [snippet 1079](http://www.djangosnippets.org/snippets/1079/). Please read its description and [this blog post](http://zerokspot.com/weblog/2008/08/13/genericforeignkeys-with-less-queries/) for any information.
This is a manager for handling generic foreign key. Generic foreign objects of the same type are fetched together in order to reduce the number of SQL queries.
To use, just assign an instance of GFKManager as the objects attribute of a model that has generic foreign keys. Then:
`MyModelWithGFKs.objects.filter(...).fetch_generic_relations()`
The generic related items will be bulk-fetched to minimize the number of queries.
**Improvement:** Problem I had with previous version from snippet 1079 : if two or more items shares the same generic foreign object, then only the first one is cached. Next ones generates new unwanted SQL queries. I solved this problem by putting all the needed foreign objects in a temporary data_map dictionary. Then, the objects are distributed to every items, so that if two items shares the same foreign object, it will only be fetched once.
This template filter converts email text to image with the email text. It uses PIL and it makes the image as high and wide as the text.
This template filter is intended to be used as **anti-spider** protection.
This command, `runtester` will run the test suite whenever files are modified. It takes the apps to test as arguments; if no apps are given the entire test suite is run.
Use this command just as `runserver` is used; fire it up in a shell and it does its thing.
Copy this snippet into `django/core/management/commands/runtester.py`.
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.