Login

Most bookmarked snippets

Snippet List

Newsletter email template

You can use this class for render and send html email message with the same logic and facility of website page creation. Just create an html template file with the same name of Class in lowercase.

  • template
  • email
  • newsletter
Read More

View all log entries in the admin

By default, you can only see the action log ("History") for particular model instances and a list of your own actions on the admin's index. This adds a fully-fledged admin view for the LogEntry model, where you can filter actions by user, content type, action type, browse by change date, and also search in the change message. Add the code to any of your apps' admin.py. The entries will be visible only to superusers and won't be editable.

  • admin
  • logentry
Read More

Image Preview on ImageField in admin

This makes a 100x100 thumbnail of the image for the image field and shows it in the admin form combination of http://djangosnippets.org/snippets/955/ and http://www.psychicorigami.com/2009/06/20/django-simple-admin-imagefield-thumbnail/

  • admin
  • widgets
  • widget
  • image-previews
  • previews-on-ImageField
Read More

Admin: return to change_list with filter and pagination applied

By default every time you change and save an object in the admin, the change_list "jumps" to the first page, so filters you used to find the object (or the pagination-page) have to be applied again. If you have to go through a multi-object-list step-by-step this could become really annoying. The above snippet changes this behaviour by returning to the referring URL when saving. Included in this URL are variables for the filters/pagination. The snippet is part of your custom Model.admin in admin.py.

  • filter
  • admin
  • pagination
  • change_list
Read More
Author: fx
  • 4
  • 8

Advanced Search in django admin

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

  • admin
  • advanced-search
  • model-filtering
Read More

Jcrop Form

Implements a Django form that integrates image uploading plus cropping using the awesome Jcrop plugin (http://deepliquid.com/content/Jcrop.html). NOTE: Still lacks proper error handling...

  • image
  • jquery
  • form
  • jcrop
Read More

Select Dropdown Widget with jQueryUI

This widget can be imported in your forms.py file and used like so: formfield = forms.ModelChoiceField(widget=SelectDropdownWidget, queryset=Model.objects.all()) The javascript is provided by [filament group's Scott and his jQueryUI Selectmenu](http://www.filamentgroup.com/lab/jquery_ui_selectmenu_an_aria_accessible_plugin_for_styling_a_html_select/). In addition to the javascript you will need [jQuery (the latest release is fine) and jQuery UI](http://jqueryui.com/). The convenient thing is that this widget will style to match whichever jQuery UI you've 'rolled' or selected, making it highly customizable! Hopefully this helps others who wanted a nice widget with jQuery and were sad to find there were no nice default options, enjoy! :)

  • dropdown
  • jquery
  • select
  • widget
  • jqueryui
Read More

Generate Model Data. Lots of Options.

Generate model data with this django management command! Data is generated based off of the model field types. And will also correctly generate foreign key's to other randomly generated records for join tables. And generate images with random colors and random words in the image - for image fields. You can supply quite a few parameters that control how the data is generated. And you can control it per field, per model. Or you can supply your own callable function which you can return your own random data. **SEE THE DOCS / EXAMPLE IN THE CODE SNIPPET FOR AVAILABLE OPTIONS, AND HOW TO CONTROL GENERATED DATA PARAMETERS** You can generate data that looks like real content, without having to write fixtures and such. Just generate it! It can generate data for these types of fields: EmailField SlugField BooleanField DateField DateTimeField TimeField IntegerField DecimalField TextField CharField IPAddressField URLField SmallIntegerField PositiveSmallIntegerField PositiveIntegerField ImageField There are also a few callables included that you can use to generate this kind of data: zip, extended zip, hashkey and uuid It's also worth noting that I keep this project up to date on my own git repository. There are a few fonts you'll need if you want to generate imaages, included in my git repo. http://gitweb.codeendeavor.com/?p=dilla.git;a=summary

  • model
  • random
  • data
  • management
  • command
  • lipsum
Read More

Reliably create a Django File using the contents of a URL

There's no direct way to save the contents of a URL to a Django File field: you're required to use a File instance but those can only safely wrap normal files, not the file-like object returned by urllib2.urlopen. Several examples online use urllib.urlretrieve() which creates a temporary file but performs no error handling without writing a ton of hackish code. This demonstrates how to create a NamedTemporaryFile, fill it with the URL contents and save it, all using APIs which raise exceptions on errors.

  • url
  • filefield
  • file
  • urlopen
Read More

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

jstree integration to django admin

You have some tree-like structure in your models, for example: `class MyModel(models.Model): parent = models.ForeignKey('self', verbose_name=u'Parent', \ null=True, blank=True, related_name='children') name = models.CharField(u'Раздел', max_lengtch=255) position = PositiveSmallIntegerField(u'Позиция', default=0) class Meta: ordering = ('position',)` To see it as a tree in admin's object list(you also can sort items, move to another parents by drag-n-drop; and rename them) add this to admin.py: `class MyModelAdmin(admin.ModelAdmin): ordering = ('position',) list_display = ('pk','name','parent','position') raw_id_fields =('parent',) list_per_page = 900 #we sould have all objects on one page list_editable = ('name','position','parent') def parent_id(self,obj): return obj.parent and obj.parent.id or '0' class Meta: model = MyModel class Media: js = [settings.MEDIA_URL + s for s in ('lib/jquery-1.3.2.min.js', 'lib/jquery.tree.min.js', 'lib/plugins/jquery.tree.contextmenu.js', 'lib/mymodel_admin.js',)] css = { 'all':(settings.MEDIA_URL+'css/nestedsortablewidget.css',) }` mymodel_admin.js is the code listed here, if you have different title field(not "name"), change var title_column in javascript, list_display and list_editable. jstree can be obtained here: [jstree](http://www.jstree.com/) screenshot is in [my blog](http://tabed.org/blog/2010/01/06/jstree-in-django-admin/)

  • admin
  • model
  • tree
Read More

Persistent connection to PostgreSQL database

Hi, I made some small custom psycopg2 backend that implements persistent connection using global variable. With this I was able to improve the amout of requests per second from 350 to 1600 (on very simple page with few selects) Just save it in the file called base.py in any directory (e.g. postgresql_psycopg2_persistent) and set in settings DATABASE_ENGINE to projectname.postgresql_psycopg2_persistent This code is threadsafe, however because python don't use multiple processors with threads you won't get bit performance boost with this one. I really recommend using it in daemon mode. In apache mod_wsgi just set processes=8 threads=1

  • database
  • connection
  • persistent
Read More

TinyMCE Widget

Widget for TinyMCE 3.2.6, a WYSIWYG HTML editor for `textarea`. **Note:** > This snippet uses the TinyMCE package thats contains special jQuery build of TinyMCE and a jQuery integration plugin. Anyway, is easily to adapt to standard package. Usage example: from django.contrib.flatpages.admin import FlatpageForm class MyFlatPageForm(FlatpageForm): content = forms.CharField(widget=TinyMCEEditor()) [TinyMCE download page](http://tinymce.moxiecode.com/download.php)

  • forms
  • wysiwyg
  • form
  • widget
  • modelform
  • tinymce
Read More

3110 snippets posted so far.