Login

Top-rated snippets

Snippet List

JSON instead of pickle for memcached

Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) doesn't know pickle, but json yes.

  • memcache
  • cache
  • json
  • memcached
  • pickle
Read More

Save image in field

Small function that i use to save files to an imagefield, the file can be either an url or a file. This function has the following requirements. import requests from django.core.files import File from django.core.files.temp import NamedTemporaryFile from django.conf import settings Get the awesome requests library: pip install requests This function uses MEDIA_ROOT to know the location of the static dir, you can change that chaging the static_dir variable.

  • image field
  • save file
Read More

SOAP web service with soaplib 2.0

I've updated the `DjangoSoapApp` class from [this popular soaplib snippet](http://djangosnippets.org/snippets/2210/) so the snippet will work properly with soaplib 2.0. Usage is the same as before: my_soap_service = DjangoSoapApp([MySOAPService], __name__)

  • soap
  • soaplib
  • wsdl
Read More

manage.py reboot

1) Simply create a "management/commands" folder in one of your INSTALLED_APPS folders. Then add a "reboot.py" file in your "management/commands" 2) In settings.py, you can OPTIONALLY add: PROJECT_NAME = 'Blah' PROJECT_DOMAIN = 'blah.com' These two settings will be used to create a "Site" object as well as a superuser. If you choose not to use these settings, the reboot command simply reverts to using your DATABASES[default] username as superuser. 3) Execute the command via "python manage.py reboot" Enjoy.

  • runserver
  • setup superuser
  • recreate database
  • install fixtures
Read More

Template filter to markup form fields with optional args

Template filter to mark-up individual form fields. Usage : In template - {% load form_custom %} then for a form field - {{ form.field|form_row:"default" }} for default wrapper or - {{ form.field|form_row:"lbl_cls=some_class_name&reqd=no" }} to pass option args seperated by & Optional args are :- wrapper_cls - override default field wrapper div class name error_cls - override default field error div class name lbl_cls - override default label_tag div class name label - yes/no default is yes - output label_tag reqd - yes/no default is yes - marks up required fields as bold with label ending with * See code for all default args.

  • filter
  • forms
Read More

Admin actions as buttons instead of a menu [v2]

Add this to your admin change_list.html template to replace the actions drop-down with buttons. This is a rewritten version of snippet [1931](http://djangosnippets.org/snippets/1931/) with Django 1.3 compatibility and cleaner code. Thanks to [andybak](http://djangosnippets.org/users/andybak/) for the idea.

  • admin
  • actions
Read More

Generic object_detail view with multiple named URL filters

This snippet is greatly inspired by [@jlorich](http://djangosnippets.org/users/jlorich/)'s useful [#2436](http://djangosnippets.org/snippets/2436/). The main difference is that I wanted to choose the names of my URL params instead of being forced into naming them "value1", "value2", etc. When reversing the URL you have to remember that the kwargs aren't friendly. By using the same names in the `filters` list, you don't have to change the way your otherwise write the URL pattern. Also it's clear throughout how you'll be filtering the QuerySet. The other change I made was "erroring early". This avoids running the QuerySet all over again inside `object_detail()` just to have it raise an exception we could have caught the first time.

  • filter
  • urlconf
  • generic-views
  • queryset
  • urlpatterns
Read More

TemplateZipFile

TemplateZipFile is a class for creating ZipFiles out of Django templates. Usage example: from zipfile import ZIP_DEFLATED from django_zipfile import TemplateZipFile def myview(request, object_id): obj = get_object_or_404(MyModel, pk=object_id) context = { 'object': obj } response = HttpResponse(mimetype='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=myfile.zip' container = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root='myapp/myzipskeleton/') container.add_template('mimetype') container.add_template('META-INF/container.xml') container.add_template('chapter1.html', context=context) container.close() return response

  • template
  • zipfile
Read More

create or update, then get, model instances from JSON/py dict

Basically the idea is to import/update model instances from a json data that closely matches the model structure (i.e. identical field names) From my answer to this question: [http://stackoverflow.com/a/8377382/202168](http://stackoverflow.com/a/8377382/202168) See the original question for sample data format.

  • json
  • import
Read More

decorator for decorators with optional args

Have you ever wanted a decorator that you could apply either straight-out: @mydec def myfun(...): ... or with special arguments: @mydec(special=foo) def myfun(...): ... ? Well, decorate it with this metadecorator, and voila. (I had this idea independently, but it's been done before as decorator_withargs: http://osdir.com/ml/python.ideas/2008-01/msg00048.html. My version is actually useful because it deals with signatures and calling directly.) As http://www.siafoo.net/article/68 points out, the standard decorator module has too much magic: the "@decorator" decorator expects a wrapping function, not a working decorator. This module fixes that.

  • decorator
  • decoratordecorator
  • metadecorator
Read More

3110 snippets posted so far.