This is a version of [http://djangosnippets.org/snippets/2258/](http://djangosnippets.org/snippets/2258/) that should work with special chars (e.g. quotes) in json data.
This is an example on how to create a custom advanced search (like the google advanced search page) in the admin app instead of the basic search field.
you need to override 2 template of the admin so in your templates/admin folder copy the html files from the original admin app as follow:
change_list.html:
add the name of the module of the templatetag at the top where there is the load command
from:
{% load adminmedia admin_list i18n %}
to:
{% load adminmedia admin_list custom_search_form i18n %}
and on line 87 (circa) you need to load our custom template tag something like:
{% block search %}{% advanced_search_form cl %}{% endblock %}
search_form.html:
put your form inside the <form> tags with {{asf}} should be enough (I have also removed cl.value from the input text field because would be to complex to reload form values too )
probably would be better to create 2 new clean template and extend them but I'll leave that as an exercise for the reader :)
many thanks to [Peter Baumgartner to get me in the right direction after his post](http://lincolnloop.com/blog/2011/jan/11/custom-filters-django-admin/)
Currently ordering in the admin panel doesn't work if you find a way to get it work please write a comment
The default traceback sent by email when an error occurs, usually gives too little information comparing it to the error page in the DEBUG mode. This snippet guerilla-patches error handling and sends by email the same information as you would see in DEBUG mode.
To set it up, add the snippet to any `models.py` of an installed app.
(I wonder why this hasn't been implemented in the core)
ResizeImageField
================
(extension of RemovableImageField)
=================================
by Wim Feijen, Go2People.
What does it do?
----------------
ResizeImageField is a replacement for django's ImageField. It has two major benefits:
1. Creation of thumbnails and scaled images.
1. Extends the image upload form and adds a preview and a checkbox to remove the existing image.
It's easy to use:
- Replace ImageField by ResizeImageField
- No further changes are necessary
Requirements:
-------------
Working installation of PIL, the Python Imaging Library
Usage
-----
- add resize_image to your app
- add resize_filters.py to your templatetags
- in settings.py, set a PHOTO_DIR, f.e. photos/original
- in models.py, add:
- from settings import PHOTO_DIR
- from resize_image import ResizeImageField
- photo = ResizeImageField(upload_to=PHOTO_DIR, blank=True)
Scaled images will be stored in 'photos/scaled',
thumbnails will be stored in 'photos/thumb'.
Access your images from your template. Add::
{% load resize_filters %}
{{ address.photo.url|thumb }}
or::
{{ address.photo.url|scaled }}
Defaults
-------
- Scaled images are max. 200x200 pixels by default
- Thumbnails are 50x50 pixels.
Override the default behaviour in settings.py
Scaling is done by PIL's thumbnail function, transparency is conserved.
Credits
------
This code is an adaptation from python snippet 636 by tomZ: "Updated Filefield / ImageField with a delete checkbox"
This creates an admin interface for each model in the "your_appname" app. It also adds each column to the admin list view.
Place this in the admin.py of "your_appname".
Adds drag-and-drop ordering of rows in the admin list view.
The only requirements is that the model has a field holding the position and that the field is made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.
The included javascript uses [jQuery UI's sortable](http://jqueryui.com/demos/sortable/) plugin
Inspired by snippets [#1053](http://djangosnippets.org/snippets/1053) and [#998](http://djangosnippets.org/snippets/998/). Another similar snippet using AJAX is [#2047](http://djangosnippets.org/snippets/2047/).
Add this code to the end of the `<body>` of your main template and it will print out all your SQL queries with timings in the Firebug console.
This uses the "django.core.context_processors.debug" template context processor, which requires that DEBUG=True and that your IP address is listed in INTERNAL_IPS.
I often find myself needing to create a template tag and all I'm interested in is the token and the render function. This decorator abstracts away the boilerplate code around creating the template node class which is the same each and every time.
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)**
You have to put this code in your *searchengines* Firefox folder in a file called **djangosnippets.xml**
After restarting Firefox, you will be able to seek DjangoSnippets very easily.
I also create a favicon.ico since djangosnippets.org didn't have one ...
Path example : /usr/lib/iceweasel/searchplugins/djangosnippets.xml
This Base64Field class can be used as an alternative to a BlobField, which is not supported by Django out of the box.
The base64 encoded data can be accessed by appending _base64 to the field name. This is especially handy when using this field for sending eMails with attachment which need to be base64 encoded anyways.
**Example use:**
class Foo(models.Model):
data = Base64Field()
foo = Foo()
foo.data = 'Hello world!'
print foo.data # will 'Hello world!'
print foo.data_base64 # will print 'SGVsbG8gd29ybGQh\n'
Here is a way to get a drop down list from a queryset, with a list of "featured" items appearing at the top (from another queryset). This can be used for long select boxes which have a subset of commonly used values. The empty label is used as a separator and values can appear in both the featured set and the full set (it's more usable if they are in both).
For example a country drop down list with 5 featured countries might look like this:
Andorra
Australia
Kazakhstan
Singapore
Turkey
------------
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
(hundreds more)
To use this, define your form field like this:
country = FeaturedModelChoiceField(queryset=Country.objects.all(),
featured_queryset=Country.objects.featured())
Put this in an __init__.py somewhere that will be executed on initialization and all errors will be printed out to stderr. Useful for debugging Facebook apps, javascript calls, etc.
Based on [this snippet](http://www.djangosnippets.org/snippets/876/). More clean, with links to the related admin forms.
Nice example on customization of contributed django admin apps.
It adds the following to the user list interface: fields for is_superuser and is_staff, last login time; by default, short names of groups user is in (mousehover to see full names) It adds the following to the to group list interface: list of users in your groups.
To enable, just put it somewhere and import it from your main urls.py: import utils/admin_auth.py
This is a ModelAdmin base class you can use to make foreign key references to User a bit nicer in admin. In addition to showing a user's username, it also shows their full name too (if they have one and it differs from the username).
**2009-08-14**: updated to handle many to many fields and easily configure whether to always show the username (if it differs from full name)
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.