Login

Most bookmarked snippets

Snippet List

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

Using another memcached for sessions

This solves the problem of losing sessions data when you restart memcached. So you use a different memcached instance for sessions which you rarely restart. Use the above code and add the following to you settings.py SESSION_ENGINE = "kwippyproject.session_backend" SESSION_CACHE = 'memcached://127.0.0.1:11200/' (Above assumes that your session's memcached is running on port 11200) Feel free to contact me in case you need help. By [Dipankar sarkar](http://dipankar.name) [email protected]

  • django
  • python
  • memcached
  • sessions
Read More

Regex Comma Number

Format Number Based on Regular Expression **Examples** >*{{.1234|regex_comma_number:'%.4f'}} >*'0.1234' >*{{100|regex_comma_number:'%i'}} >*'100' >*{{ 234.5678|regex_comma_number:'%.4f'}} >*'234.5678' >*{{234.5678|regex_comma_number:'$%.4f'}} >*'$234.5678' >*{{1000|regex_comma_number:'%i'}} >*'1,000' >*{{1234.5678|regex_comma_number:'%.4f'}} >*'1,234.5678' >*{{1234.5678|regex_comma_number:'$%.4f'}} >*'$1,234.5678' >*{{1000000|regex_comma_number:'%i'}} >*'1,000,000' >*{{1234567.5678|regex_comma_number:'%.4f'}} >*'1,234,567.5678' >*{{1234567.5678|regex_comma_number:'$%.4f'}} >*'$1,234,567.5678' >*{{-100|regex_comma_number:'%i'}} >*'-100' >*{{-234.5678|regex_comma_number:'%.4f'}} >*-234.5678' >*{{-234.5678|regex_comma_number:'$%.4f'}} >*'$-234.5678' >*{{-1000|regex_comma_number:'%i'}} >*'-1,000' >*{{-1234.5678|regex_comma_number:'%.4f'}} >*'-1,234.5678' >*{{-1234.5678|regex_comma_number:'$%.4f'}} >*'$-1,234.5678' >*{{-1000000|regex_comma_number:'%i'}} >*'-1,000,000' >*{{-1234567.5678|regex_comma_number:'%.4f'}} >*'-1,234,567.5678' >*{{-1234567.5678|regex_comma_number:'$%.4f'}} >*'$-1,234,567.5678'`

  • templatetag
  • regex
  • format
  • comma
  • number
Read More

Simple FastCGI authorizer view

This is a basic view for a FastCGI authorizer against the Django auth. The idea is to return either a blank response with REMOTE_USER set on success, a forbidden response for failure, or a redirect to a login page when no user is logged in. I use this view for a Trac instance running on the same (lighttpd) server as Django. lighttpd is set up to use Django as a FastCGI authorizer (using snippet 1149) for the Trac URLs instead of using basic/digest HTTP authentication, so Trac has the same users as Django.

  • authenticate
  • fcgi
  • fastcgi
Read More
Author: cme
  • 0
  • 3

View and StatefulView classes

This snippet provides two view classes. The two reason I wanted to write this are, 1) Not have to import all the boilerplate code for each view and 2) so I could have the same URL handle loading a persons profile, or handle an OpenID login request without having to write two separate views. (Yes I know it isnt to hard to write my view, check the header and pass it off to the proper handler view, but I think it looks better code wise to have the handler methods all in one class) The first one is just for normal views conveniently called *View*. The *View* class that lets you do at least 90% of what you can do in a normal view function, but without having to import all the normal boilerplate code first since this class wraps methods around most if not all the *HttpResponse* types. The second class *StatefulView* maintains its state across page loads This is especialy useful for ajax type calls where you wish to maintain some form of state while the user is doing something but do not wish to make DB calls and do not wish to polute the session with trivial things **Note:** On my system it maintains state across browsers and computers as it is not tied to the session, BUT for this to happen all requests must be handled by the same proccess. So requests going to a differing process with not have the state maintained.

  • views
  • class
  • stateful
Read More

simple email validation function

This very basic function I was missing in this part of the Django Documentation when creating a custom MultiEmailField: http://docs.djangoproject.com/en/dev/ref/forms/validation/

  • email
  • validation
Read More

Photologue wiki-syntax templatetag

**updated 12/16/08** I run several blogs by visual-artists and web-designers who want a quick way to insert images into their blog without the awkwardness of WSYIWYG and without the technicality of code (eww...). Thanks in advance for your input. #Syntax in a blog goes: [[thumb:the-image-slug]] # Gives you a thumbnail [[image:the-image-slug]] # Presents full-size-image Then of course: {% blog.post|yeagowiki %} You will also need to create some templates (see snippet). Here's a sample: <!-- /templates/photologue/image_snippet.html --> <div class="photologue-image"> <a href="{% if url %}{{ url }}{% else %}/media/{{ image.image }}{% endif %}"> <img src="{{ image.get_display_url }}" /> </a> <p class="photologue-image-caption">{{ image.caption }}</p> </div>

  • templatetag
  • markdown
  • wiki
  • photologue
Read More

CSRF this!

A form with built-in CSRF protection. Include CsrfCookieMiddleware in your MIDDLEWARE_SETTINGS, subclass SafeForm and off you go. See: [this django-developers post](http://groups.google.com/group/django-developers/browse_thread/thread/2c33621003992d07?hl=en) for more info. [edit] This form is actually WAY overengineered currently. Will update soon.

  • forms
  • csrf
Read More

Session Wizard

This a wizard tool similar to the one in django.contrib.formtools, but it uses a session. I hope to eventually get this into shape and contribute it to the formtools package, but for right now, here it is. The wizard steps are broken into 2 categories: Show Form and Submit Form Each category has it's own hooks for manipulating the process, for instance you can short-circuit a process_submit_form() and go straight to done() via a return from preprocess_submit_form(). Sorry for the lack of documentation on this. Here's an example urls.py entry : `(r'^estimate/(?P<page0>\d+)/$', EstimateWizard([EstimateCustomer, EstimateJob, EstimateRoom]))` where EstimateCustomer, Job, and Room are Form classes and EstimateWizard extends SessionFormWizard

  • session
  • form
  • wizard
Read More

3110 snippets posted so far.