Login

All snippets written in Python

Snippet List

Radio widget with labels after inputs

Difference from standard Django 1.4 implementation is just structure of list. In django `<input>` elements are *wrapped* by their labels, and look like this:: <ul> <li><label><input/> Label 1</label> <li><label><input/> Label 2</label> </ul> This widget puts labels *after* input elements:: <ul class="form-button-radio"> <li><input/> <label>Label 1</label> <li><input/> <label>Label 2</label> </ul> It makes possible to define style for both inputs and labels, hiding inputs and making labels look like pressable buttons. No javascript needed, just CSS (see docstring). To auto-submit the form when pressing a button, JQuery can be added:: <script> $(document).ready(function() { $('ul.form-button-radio input[type="radio"]').change(function() { $(this).parents('form').submit(); }); }); </script>

  • widget
  • label
  • input
  • button
  • reorder
  • radio
  • radio button
Read More

RegEx redirect fallback middleware

Simple middleware to complement the built in redirect middleware app. Add this after the contrib.redirect middleware - this will be fired if a 404 is triggered and the contrib.redirect fails to find a suitable redirect. Useful if you want to add the redirects into the DB - and/or don't have access to the .htaccess script or whatever HTTP server based redirect machinery your site runs off. You simply add in regex 'old_path' and 'new_path' entries into the same redirect app, but this will try to do a regex find and replace - i.e. >>> r = new Redirect() >>> r.old_path = '/my/test/path/([a-z]+)/with/regex/' >>> r.new_path = '/my/result/path/$1/with/regex/' >>> r.save() this will convert: /my/test/path/abcdef/with/regex/ into: /my/result/path/abcdef/with/regex/' also works with query strings: /index.php?section=products >>> old_path = '/index.php/\?section=([a-z]+)' #need to add in the forward slash if ADD_SLASHES = True in your settings, and escape the question mark. >>> new_path = '/section/$1/' converts the url to '/section/products/'

  • regex
  • Redirect
  • /
Read More

template filter to include protocol and domain in absolute urls

I created this template filter to be able to use get_absolute_url in an email template. Save the code into /templatetags/navigation.py Use like this: {% load navigation %} {{ instance.get_absolute_url|siteabsoluteurl:request }}

  • template
  • filter
  • templatetag
  • template-filter
  • url
  • template-filters
  • get_absolute_url
  • templatefilter
  • absolute
Read More

Cache Backend using memcached including prefix settings

Django later than 1.3 (not sure of exact version) wasn't using prefix settings in cache tags or functions used in views. Just for whole page caching. This is small custom cache snippet based on memcached.CacheClass. Feel free adding any comments.

  • memcache
  • cache
  • memcached
  • cache-backend
  • prefix
Read More

Closure for FieldListFilter classes with custom sets of ranges

This closure lets you quickly produce date-style range filters in the Django Admin interface without having to create a new class for each one. It follows Python range semantics, with the lower value using a `_gte` test and the upper value using an `_lt` test. Here's an example of how I'm using it in one of my projects: list_filter = ('complete', ('chapters', makeRangeFieldListFilter([ (_('1'), 1, 2), (_('2 to 10'), 2, 10), (_('11 to 30'), 11, 30), (_('31 to 100'), 31, 100), (_('At least 100'), 100, None), ], nullable=True)), ('word_count', makeRangeFieldListFilter([ (_('Less than 1000'), None, 1000), (_('1K to 5K'), 1000, 5000), (_('5K to 10K'), 5000, 10000), (_('10K to 75K'), 10000, 75000), (_('75K to 150K'), 75000, 150000), (_('150K to 300K'), 150000, 300000), (_('At least 300K'), 300000, None), ], nullable=True)), ('derivatives_count', makeRangeFieldListFilter([ (_('None'), 0, 1), (_('1 to 5'), 1, 5), (_('5 to 50'), 5, 50), (_('50 to 1000'), 50, 1000), (_('At least 1000'), 1000, None), ])), 'pub_date', 'upd_date') It is based on code from `DateFieldListFilter` and `BooleanFieldListFilter` from `django.contrib.admin.filters`.

  • filter
  • django
  • admin
  • fieldlistfilter
Read More

IsNullFieldListFilter

As the title does a pretty good job of condensing, this is a subclass of `FieldListFilter` for the Django 1.4 Admin system which allows you filter by whether a field is or is not `NULL`. For example, if you had an `Author` model and wanted to filter it by whether authors were also users of the site, you could add this to your `AuthorAdmin` class: list_filter = ( ('user_acct', IsNullFieldListFilter), ) For the record, it began life as a modified version of `BooleanFieldListFilter` from `django.contrib.admin.filters`.

  • filter
  • django
  • admin
  • fieldlistfilter
Read More

drupal7 password check

Completely based on [snippet 2729](http://djangosnippets.org/snippets/2729/) (see that snippet for useful comments!). The above snippet did not work for me (something with MemoryError), so I looked at the Drula source code and reimplemented...

  • password
  • hash
  • drupal7
Read More

add port from settings file to an url

I'm working on a Project where on certain places I need absolute URL's, in development mode I need the port 8000 added to any absolute url. This piece of work, took me some time to figure out. Couldn't find something similiar on the net, it's based on Code from the Python urlparse module. You can change the "settings.PORT" part to "settings.DEBUG == True" if you like, and so on. META: replace parameters in URL, edit parameters in URL, edit URL, urlparse

  • url
  • urlparse
  • port
  • replace-parameters-in-url
  • urlsplit
Read More

get all models name

Veritabanına eklenmiş tüm models isimleri Get all models name kullanımı / usage: {% load moduller %} {% moduller %}

  • get
  • all
  • models name
  • get_models
Read More

TreePrint: Print nested lists, tuples, dicts

Displays nested lists / dicts / tuples in an aligned hierarchy to make debugging easy. Accepts all variable types, including nested lists with any mixture of tuples / dictionaries / lists / numbers / strings. Analogous to the PHP print_r($var) function.

  • nested
  • nested-set
  • nested-dict
  • print_r
  • nested-list
Read More

split in html template

Html içinde split ile kesme Kesme işleminden sonra kaçıncı bloğun okunacağını düzeltebilme {% kes request.path "/" 3 4 %} gelenveri.split("/")[3:4]

  • template
  • html
  • simple_tag
  • split
Read More

2955 snippets posted so far.