Login

Most bookmarked snippets

Snippet List

MultiSelectField with comma separated values (Field + FormField)

Daniel Roseman's snippet, updated will all fixes mentioned in the comments of the first version + some other things to make it work under Django 1.4. South, and dumpdata are working. There's an ugly int(....) at the validate function in order to cast each value as an integer before comparing it to default choices : I needed this, but if you're storing strings values, just remove the int(......) wrapper. ------------------------------------- Orginal readme Usually you want to store multiple choices as a manytomany link to another table. Sometimes however it is useful to store them in the model itself. This field implements a model field and an accompanying formfield to store multiple choices as a comma-separated list of values, using the normal CHOICES attribute. You'll need to set maxlength long enough to cope with the maximum number of choices, plus a comma for each. The normal get_FOO_display() method returns a comma-delimited string of the expanded values of the selected choices. The formfield takes an optional max_choices parameter to validate a maximum number of choices.

  • checkbox
  • multiple
  • forms
  • model
  • field
  • comma
Read More

Apache X-sendfile with permissions checking

This allows the mod_xsendfile module for Apache safely serving private files. Django take cake about processing and permissions checking, Apache server requested files. Installation of mod_xsendfile: $ tar -xzvf mod_xsendfile-0.12.tar.gz $ /usr/sbin/apxs -c mod_xsendfile-0.12/mod_xsendfile.c $ ld -Bshareable -o mod_xsendfile-0.12/mod_xsendfile.so mod_xsendfile-0.12/mod_xsendfile.o Copy mod_xsendfile.so to your local Apache modules folder. Modify httpd.conf to load an enable the module: LoadModule xsendfile_module modules/mod_xsendfile.so Add to virtual host container: <Virtual ...:80> XSendFile On XSendFilePath /home/django_projects/mysite/media/ </Virtual>

  • http
  • media
  • static
  • xsendfile
Read More

Easier and Faster than flatpages. Rendering templates by simpling calling by his path on the URL

This is in my opinion a better way to have flat pages in a project. In the example with the url patterns settings: / will render -> /pages/welcome.html /contact will render -> /pages/contact.html /products/ will render -> /pages/products/index.html /products/pricing will render -> /pages/products/pricing.html

  • views
  • flatpages
  • class-based-views
  • 1.4
  • class-based-views_1
Read More

Test Suite URL Coverage

This custom test suite runner will record all of the URLs accessed during your test suite. It will compare it to the list of all URLs you have configured for your site and produce a report of any URLs missed. It requires that all URLs are named (using the ``name=`` parameter). To use is, set the ``TEST_RUNNER`` variable in your configuration to this class. You can also define ignored URLs. For example, to filter out the admin URLs, you can use: IGNORED_COVERAGE_URLS = ['^admin/', '^admin/doc/']

  • urlconf
  • testing
  • testrunner
Read More

Improved Button Admin

An improved version of http://djangosnippets.org/snippets/1016/ which lets you add separate buttons for change_list and change_form pages in the admin.

  • admin
  • buttons
Read More

Updated: GeoJSON Serializer for GeoDjango (gis)

Unfortunately the built in Django JSON serialzer encodes GeoDjango GeometyrField as WKT text. This snippet extends django's serializer and adds support for GEOJson format. Built in JSON serializer output: [{"pk": 1, ... "geopoint": "POINT (-76.5060419999999937 44.2337040000000030)" ... }] GeoJSON serializer ouput: [{"pk": 1, ... "geopoint": {"type": "Point", "coordinates": [-76.503296000000006, 44.230956999999997], "__GEOSGeometry__": [ "__init__", [ "SRID=4326;POINT (-75.5129950000000036 44.2442360000000008)" ] ] }] Note: the "__GEOSGeometry__" is a class hint as defined by JSON-RCP and used during deserilization.

  • GeoJson
  • Serializer
  • Deserializer
Read More

Get Client IP Behind Proxy

If your application server is behind a proxy, `request.META["REMOTE_ADDR"]` will likely return the proxy server's IP, not the client's IP. The proxy server will usually provide the client's IP in the `HTTP_X_FORWARDED_FOR` header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).

  • request
  • ip
Read More

Filter by taggit tags in the admin

A FilterSpec that can be used to filter by taggit tags in the admin. To use, simply import this module (for example in `models.py`), and add the name of your TaggableManager field in the list_filter attribute of your ModelAdmin class.

  • filter
  • admin
  • taggit
Read More

User groups template tag

This snippet is an improved version of the [ifusergroup](http://djangosnippets.org/snippets/1576/) tag that allows spaces in any of the group names. It also fixes a small bug where if a group didn't exist none of the subsequent groups would be checked.

  • tag
  • templatetag
  • user
  • auth
  • group
  • permissions
  • access-control
Read More

easy signal registration

Django signals can live anywhere you like. I like them in *app/signals.py* file. This snippet imports signal modules from all installed apps.

  • registration
  • signal
Read More

Math Captcha Field and Widget

This math captcha field and widget was inspired by Justin Quick's django-math-captcha, but I wanted to make it into one form field and not have anything in settings.py. I removed the division and modulo operators to avoid people having to input fractions, and just randomly select the operator. It leverages Django's built-in MultiValueField to set up the hidden hashed answer to compare the user's input to instead of rendering strings for the fields like django-math-captcha does. Unit tests soon to follow, but this is used in production at: http://btaylorweb.com/. Enjoy!

  • simple
  • captcha
  • field
  • widget
  • math
  • multiwidget
  • multifield
Read More

3110 snippets posted so far.