Login

Top-rated snippets

Snippet List

Admin actions as buttons instead of a menu

Add this to any admin changelist and your actions drop-down will be replaced with user-friendly buttons. Why mess around with templates and subclassing admin classes when you can just mangle the page with jQuery! ;-) It also adds a 'select all' label to explain the mystery top check-box (well it was a mystery to several of my clients). The line "if ($('div.actions option:gt(0)').length<=8)" checks that there aren't more than 8 actions and falls back to the drop-down if there are. Requires jQuery to be loaded.

  • admin
  • actions
Read More

Context processor for django admin app_list

I never found a good snippet or tutorial to get the app_list (in django.admin) on other pages except the index page. So i started asking on irc j00bar has given me a very nice answer, but first i didn't know what to do with it till now. Anyways this snippet is very handy for the people who wants this but don't know how to get it. This is special made for the django version 1.1 Installation is quite easy, it is a context processor, so download this file put anywhere in your project, i got it in a app called cms_theme (theme and template related stuff.) and put the location in your settings.py file, example: `TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.request', '****cms.cms_themes.context_processors.theme', '****cms.cms_themes.context_processors.applist', )` The '****' stuff is nothing, i replaced my company name with it. Of course you may put the context processor anywhere else, that is your choice. Good luck! Alexander

  • django
  • models
  • admin
  • python
  • applications
  • app_list
  • app-models
Read More

Log to syslog

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

  • logging
  • syslog
Read More

Percent Field

This can be used in a forms.Form or forms.ModelForm in django. Render a decimal field between 0 and 1 into a value between 0 and 100, and vice-versa.

  • field
  • percent
Read More

Foreign Key list_filter wthout custom FilterSpec

This is some (probably) pretty dodgy code that allows for foreign keys in the admin's list_filter without using a custom FilterSpec. It overrides the django.db.models.options.Options get_field class to handle spanned relationships, i.e. list_filter=('house__room__town',) as seen in http://code.djangoproject.com/ticket/3400 I have only tested this with a double foreign key relationship('house__room'), and it worked. It hasn't been tested in production, and the troubling part of this code is probably to use of 'deepcopy'. I do add those copies back to the options instance, so there shouldn't be too much runaway copying.

  • foreign-key
  • filterspec
  • list-filter
  • 3400
Read More

Unzip a .zip file uploaded with FileBrowser

Django Filebrowser (http://code.google.com/p/django-filebrowser/) provides a signal called filebrowser_post_upload. Adding this method to a model that has a FileBrowse field will cause uploaded .zip files to be detected and unzipped on the server in place, then deleted (leaving their folder behind). It will also delete the garbage __MACOSX folder created by Mac zip files. This is probably not safe to do for publicly uploaded files. The use case here was to allow journalists to upload entire SoundSlides projects into a custom CMS.

  • filebrowser
  • zip
  • unzip
Read More

Updated Filter to resize a ImageField on demand (ver.2)

An update to snippet 1718 (http://www.djangosnippets.org/snippets/1718/). This update lets you pass an image URL string as well as a model ImageField instance. This means that you can then create thumbnails on demand for images outside of model

  • image
  • templatetag
  • thumbnail
  • resize
  • imagefield
Read More

Fieldsets for Views

This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin. How to Use: In the view in question, put: def some_view(request): ... fieldsets = ( (u'Title 1', {'hidden' : ('field_1', 'field_2',), 'fields' : ('field_3',)}), (u'Title 2', {'hidden' : ('field_5', 'field_6',), 'fields' : ('field_4',)}),) ) return render_to_response('some.html', {'fieldsets': fieldsets}) fieldsets = ( (None, {'hidden' : ('evento', 'colaborador',), 'fields' : ('acompanhantes',)}), ) Next, in the html just add: <form enctype="multipart/form-data" id="edit" method="post" ...> ... {% include "inc/form_snippet.html" %} ... <input type="submit" value="Submit"> </form>

  • template
  • django
  • admin
  • views
  • filters
  • python
  • tags
  • html
  • css
  • dicts
Read More
Author: Nad
  • 2
  • 0

Remember me for login

Set session to never expire in settings, and when remember_me is found false in login POST, set it to browser session expiry. Works only in Django 1+.

  • views
  • python
  • login
  • rememberme
Read More

HibernateBooleanField

Sometimes its necessary to map your django models to Java Hibernate created tables. Hibernate maps boolean field to bit(1) column instead of tinyint in django. NOTE: tested for mysql backend only

  • model
  • field
  • hibername
Read More

"Save and Continue" keyboard command for admin, with autoscroll

This snippet is helpful if you do a lot of editing on a single large admin form (for example, in a rich text field), and want to frequently save your progress. If you press control-S, or command-S on a Mac, the admin change form will save and reload, and the page will scroll back down to where you last were. This snippet relies on jquery, [jquery.cookie](http://plugins.jquery.com/project/cookie), and the [shortcut.js](http://www.openjs.com/scripts/events/keyboard_shortcuts/) keyboard library (which doesn't use jquery, but seemed more robust than the jquery keyboard plugins I saw). It uses a temporary cookie to remember where the page was scrolled to, to avoid having to override the admin behavior. Note: don't put this in templates/admin/change_form.html -- the circular import causes a Django crash. *Edit: Had forgotten to include jquery.cookie, which I was already including elsewhere.*

  • admin
  • jquery
  • save
Read More

3110 snippets posted so far.