Login

All snippets

Snippet List

New Pattern

Now you can use object-oriented URL resolve and include and you can add function cp_before wich will be running before any any cp__* functions in his module

  • url
  • resolving
  • object-oriented
  • newpattern
  • pattern
Read More

Restructuredtext directive for photolouge

Directive for inserting images using photolouge in django. Usage: Just make a .py with this code and import it in some apps models.py Or make a custom formatter with django-template-utils.

  • image
  • insert
  • rst
  • directive
  • photolouge
Read More

FilterManager

Save a filter in admin app Example: http://localhost:8000/admin/org/registrationprofile/?title__exact=Dr&ot=asc&o=4&speciality__exact=gynaecology you can save this path org/registrationprofile/?title__exact=Dr&ot=asc&o=4&speciality__exact=gynaecology as your filter with name like "DrGynecologySortedCyty" and then select this filter from selectbox include JavaScript file FilterManager.js and jQuery in all admin templates. === Install === 1. Add this in header of base.html for contrib.admin You can download this files from http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.2.6.min.js 2. Change path for your model that you will save in javaScript file FilterManager.js Example: /admin/org/registrationprofile/ 3. Add models and views. see code

  • filtermanager
Read More

Preferred Domain decorator function.

From time to time I often have to work with a site that has two domain names. This can be an issue when dealing with mapping api keys so to solve this problem I whipped up a decorator function that allows a preferred domain to be enforced on a per view basis. It wouldn't be too much to take this onto a middleware, but for my usage I just wanted a per view granularity and a decorator function suited my needs quite nicely.

  • decorator
  • domain
  • preferred-domain
Read More

Pygments Syntax highlighting template tag

This template tag will attempt to apply pygments syntax highlighting to anything inside {% code %} and {% endcode %} tags. You can optionally pass in the language to highlight in the form of {% code 'lang' %} - if you don't, it will first try to guess the syntax, and then fall back to Python syntax highlighting.

  • templatetag
  • pygments
  • highlighting
  • syntax
Read More

Safer cache key generation

If any cache keys you generate include user (staff or public) supplied data, they may: be too long, contain invalid characters (at least by memcache's standards), or both. This helper will sub out any invalid characters and md5 the key if it's too long. Additionally, it'll insert the CACHE_MIDDLEWARE_KEY_PREFIX from django.conf.settings for you. In your memcache instances are used by multiple applications (Django or otherwise) this can help ensure your keys are unique to that a particular app on a particular site.

  • memcache
  • cache
  • caching
Read More

django simple search

simple search with Q object you just pass a list of fields and the search string then it will come up with a object to use in a filter. This snippet is thanks to Julien Phalip at [julienphalip.com](http://www.julienphalip.com/blog/2008/08/16/adding-search-django-site-snap/)

  • search
Read More

Example usage of SimpleCouchPaginator

Example Usage of [Very basic CouchDB Paginator](http://www.djangosnippets.org/snippets/1208/) An Example setup of CouchDB with django can be found at: [Eric Florenzanos post about django+couchdb](http://www.eflorenzano.com/blog/post/using-couchdb-django/)

  • pagination
  • paginator
  • couchdb
  • example
Read More

Basic CouchDB Paginator (Updated)

This is a very basic paginator for CouchDB ViewResults. It's usage is similar to the django one. **CouchPaginator** is almost like the django version with one exception: It's constructor needs an additional argument `pages_view` which is either a reduce result or an integer which holds the total number of objects across all pages. *Example Map/Reduce for pages*: > map = function(doc) { > if(doc.type=="application" || doc.type==\"inquiry\"){ > emit([doc.meta.user.toLowerCase(), doc.type], 1); > } > reduce = function (doc, value, rereduce){ > return sum(value); > } **SimpleCouchPaginator** is much simpler and needs no total number and can only paginate by "next" and "previous" page. Use this if you don't have an reduce view which tells you the amount of total object. An Example setup of CouchDB with django can be found at: [Eric Florenzanos post about django+couchdb](http://www.eflorenzano.com/blog/post/using-couchdb-django/) [See example usage](http://www.djangosnippets.org/snippets/1209/)

  • pagination
  • paginator
  • couchdb
Read More

UKPhoneNumberField

Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890" Can reject premium numbers (09123 123123) or service numbers (1471, 118 118) with `UKPhoneNumberField(reject=('premium', 'service'))` Uses info from [Wikipedia](http://en.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom)

  • form
  • field
  • uk
  • localflavor
Read More

SplitSelectDateTimeWidget

This class extends MultiWidget to create a widget that consists of HTML select elements for Django's forms.DateTimeFields. This results in a select elements with options for month, day, year, hour, minute, second, and (if using the twelve_hr option) meridiem. # Default usage of SplitSelectDateTimeWidget class TimeForm(Form): dt = DateTimeField(widget=SplitSelectDateTimeWidget()) Another example hooks into the flexibility of the underlying Select Widgets: class TimeForm(Form) dt = DateTimeField(widget=SplitSelectDateTimeWidget(hour_step=2, \ minute_step=15, second_step=30, twelve_hr=True, years=[2008,2009,2010])) The above example displays hours in increments of 2, minutes in increments of 15, and seconds in increments of 30. Likewise, only the years 2008, 2009,and 2010 are displayed in the years' options.

  • forms
  • widgets
  • widget
Read More

Date Picker Template Include

All you need to do is include the file you save this as in your other templates. The css and javascript is right in the file. Fully customizable. Very easy. Click a date to choose a single date. Click a second date to choose a range. A third click will act as a first click. note: you also will want to customize the django template specific variables in elements with ids: s_date e_date cal_month if you change those, it's not even really django specific, but i use this in my form for time off requests at work.

  • template
  • date
  • picker
  • include
Read More

Rails-like MVC Controllers for Django

[See the blog entry](http://sciyoshi.com/blog/2008/nov/18/rails-mvc-controllers-django/) Allows using controllers for views. This allows for nice subclassing to override behavior of views. `Controller.urls` (see below) works fine for subclasses as well. Similar to [snippet #1165](http://www.djangosnippets.org/snippets/1165/) except that it won't break reverse URL resolving and regex validation in URLs. In `views.py`: import mvc class MyController(mvc.Controller): @mvc.view('myview/$', 'myview') def my_view(self): # do something with self.request return HttpResponse('something') class Meta(mvc.Controller.Meta): url_prefix = 'mycontroller-' In `urls.py`: from . import views urlpatterns = patterns('', # ... other urls here ... *views.MyController.urls(r'prefix/') ) Then the view `MyController.my_view` will be accessible from `'prefix/myview/'` and have the name `'mycontroller-myview'`, i.e. `reverse('mycontroller-myview')` will work as expected.

  • rails
  • controller
  • mvc
Read More

3110 snippets posted so far.