Login

All snippets

Snippet List

Make anything into a template

This is a quick and dirty way to reuse the Django templating system for your own ends. Just pop in the familiar Django template syntax into whatever content you like and any chunk of content can be a template. This is great if you need to wrap data in HTML (as in for a mass email). The best part about this is that your "template" can now be stored in a database, instead of just in the file system.

  • template
  • templating
Read More

Convert CamelCase to lowercase_with_underscores

Just a simple regex function to convert a camel case string ("ClassName") to a lower case string with underscores ("class_name"). Came across a need for this when I was trying to add properties to a model at runtime using `object.__class__.__name__`. This is a modification of the function "get_verbose_name" found in django.db.models.options.

  • utilities
  • formatting
  • one-liners
Read More

AjaxCheckMiddleware

Simply adds an attribute `is_ajax` to a request object, indicating if the request was made via Ajax. Allows you to reuse a lot of POST processing view code to which you'd like to progressively add Ajax: `if request.is_ajax: return JsonResponse(some_json)` `else: return render_to_response('some_template.html')`

  • middleware
  • ajax
  • request
Read More

MultiFileWidget

This is a multi file upload widget. That does not require adding multiple file input fields. It requires jQuery.MultiUpload (http://www.fyneworks.com/jquery/multiple-file-upload/)

  • newforms
  • upload
  • multi-file-upload
  • file
Read More

Generic Views for newforms

Two common generic views to be used with newforms in similar way django.views.generic.create_update views, but adapted to the newforms library.

  • newforms
  • view
  • generic
Read More

Add special field lookups to the Admin list_filter display

Ever wanted to add an atypical [field lookup](http://www.djangoproject.com/documentation/db-api/#field-lookups) to the Django Admin list_filter filters, like `__isnull` or `__in`? This jQuery snippet allows you to do just that. Since you can access those additional filters by directly typing them into in the Admin URL, the tricky part is to add those to the regular list_filter display. A lot of this code is spent checking on querystring matches which is ugly and error-prone -- if you see any problems or room for improvement, drop me a comment! A suggestion of where to place this code is in `templates/admin/yourapp/yourmodel/change_list.html`. Mine kinda looks like this: {% extends "admin/change_list.html" %} {% block content %} <script src="/static/js/jquery-1.2.2.min.js" type="text/javascript"></script> <script type="text/javascript"> // the JavaScript posted in this snippet </script> {{ block.super }} {% endblock %}

  • admin
  • jquery
Read More

Export Database and Media_Root via Admin Interface

This is the view-code to export a database-dump (hardcoded for mysql) or a media_root dump via the admin interface. just add 2 urls patterns to call the views and a small template with a simple form to send a http-post to the views. Note: The downloads are sort of streaming. I have successfully exportet a 2GB media_root as tar-ball without major increase of ram-usage of the django-process.

  • admin
  • export
  • database
  • media-root
Read More

SimpleMachines forum authentication backend

Authentication backend for Simple Machines Forum user database. Needs one setting in `settings.py`: SMF_PREFIX = 'smf' This is prefix of SMF tables. This shippet assumes that they are in the same database. There is one more optional setting: SMF_GROUP = 1 If set, will allow only users from group with given id. May be used to allow access to admin page only for moderators.

  • authentication
  • backend
  • smf
Read More

FCKWidget for NewForms

This is a simple FCK editor widget that can be used in newforms in place of Textarea. Obviously it requires [FCKeditor](http://www.fckeditor.net/) and you will need to set the proper import path for that.

  • newforms
  • rich-text-editor
  • wisiwig
Read More

Regrouping admin models

This is modification of Django's original adminapplist template tag. You can move your models from one app to other or completely hide them with this mod. Copy django_dir/contrib/admin/templates/admin/index.html file to your templates/admin folder, open it, then change {% load adminapplist %} to {% load custom_adminapplist %} (or whatever you named the templatetag file) After that, write your regrouping schema to settings.py file like this; UPDATED, now using tupples instead of dicts in APP_SCHEMA to make it more DRY. ` APP_SCHEMA=[ ( ['Model','List'], 'From App', 'To App', ), ( ['FlatPage'], 'Flatpages', 'Site Content', ), ( ['Product'] 'Product', 'Shop', ), ( ['Site'] 'Sites', #We are hiding Site model by not defining a target. ), ] `

  • templatetag
  • models
  • admin
  • app
Read More

Automagically import settings from installed applications

Use this snippet at the end of your main settings.py file to automagically import the settings defined in each app of `INSTALLED_APPS` that begins with `APPS_BASE_NAME`. Set `APPS_BASE_NAME` to the base name of your Django project (e.g. the parent directory) and put `settings.py` files in every app directory (next to the `models.py` file) you want to have local settings in. # works in the Django shell >>> from django.conf import settings >>> settings.TEST_SETTING_FROM_APP "this is great for reusable apps" Please keep in mind that the imported settings will overwrite the already given and preceding settings, e.g. when you use the same setting name in different applications. Props to [bartTC](http://www.djangosnippets.org/users/bartTC/) for the idea.

  • settings
  • import
  • reusable
  • apps
  • applications
Read More

A custom 500 handler which is AJAX-aware

The default 500 handler does not take into consideration if the HTTP request made is an XMLHttpRequest. If it's an XHR, then it may not be a good idea to send back your default 500 HTML page. To use this custom 500 handler, just add the following to your urls.py: handler500 = 'my_project.my_app.views.my_500'

  • ajax
  • 500
  • handler
  • httprequest
Read More

Color SQL logging middleware

Yet another SQL logger, this time with color and tresholds. By default it will only print a summary line (total time, query count) unless one of the tresholds (total time, query count, individual query time) was exceeded. You may use LOG_COLORSQL_VERBOSE = True to get the raw SQL for all requests regardless of any configured tresholds. In either case the middleware will highlight problematic areas in yellow or red. If you don't like the colors you can either set your tresholds to really high values, edit the code, or use a different middleware. :-)

  • sql
  • middleware
  • log
Read More
Author: moe
  • 4
  • 13

HTML Prettifier

Tired of scrolling through hundreds of lines of code where the indentation is maddening? Here's a middleware class that prettifys your html markup so it's nice and consistently indented. Intended only for debugging, and I add it to the middleware stack conditionally on TEMPLATE_DEBUG. Requires BeautifulSoup.

  • html
  • pretty
  • prettify
  • ugly
  • indented
Read More

3110 snippets posted so far.