Login

All snippets written in Python

2956 snippets

Snippet List

Month list for a select drop down

month_ids is a list of months like this... [('Apr07', 'April 2007'), ('Mar07', 'March 2007'), ('Feb07', 'February 2007')] which can be used in a select box like this.. month = forms.ChoiceField(choices=(months))

  • months
  • select
Read More

Slice filter

Handy for things such as: `{{ some_string|slice:","|join:";" }}`

  • filter
  • string
  • slice
  • simple
Read More

Basic HTTP Authentication

A patch (against django svn trunk [4649](http://code.djangoproject.com/browser/django/trunk/?rev=4649)) that allows users to log in with Basic HTTP Authentication i.s.o. login forms using some simple middleware (entire patch is ~50 lines). I was unaware of http://code.djangoproject.com/wiki/GenericAuthorization so I'm not sure about its usefulness in the long run. You can enable it by including 'django.contrib.auth.middleware.BasicAuthenticationMiddleware' in your MIDDLEWARE_CLASSES and then adding the following lines in your settings.py: BASIC_WWW_AUTHENTICATION = True WWW_AUTHENTICATION_REALM = "djangolures.com" Updated: See also http://code.djangoproject.com/ticket/3609 (patch now availble here as well).

  • middleware
  • django
  • http
  • basic
  • authentication
Read More

Manage.py alterdb command

added commands: altersql - shows sql code with alter queries alterdb - apply alter queries. parameters: --showsql - show queries --app=APPLICATION - alter only selected application [you need clone this repo](https://bitbucket.org/certator/django_snippets)

  • hack
  • table
  • syncdb
  • alter
Read More

View Redirect Decorators

Temporary and permanent view redirect decorators. Simplifies views that always redirect to a specific or configured url. You can use the reverse() function (or any other runtime-required lookup) via `lambda` to define the redirect url.

  • view
  • decorator
  • redirects
Read More

Detect iPhone & Switch Template via render_to_response

A fast way to implement an iPhone template switcher, especially if you have a lot of existing views using the render_to_response() shortcut. This checks for the iPhone browser and then modifies the chosen template by adding -mobile to the html's file name. Check out [this more complete list of user agents](http://minidetector.googlecode.com/svn/trunk/minidetector/tests/mobile_useragents.txt) if you need to detect specific mobile devices.

  • iphone template render_to_response
Read More

custom template tag sample

usage: <div class="content_box" id="tests" style="background:#cfc"> {% load extra %} <p>{% get_current_time '%Y-%m-%d %I:%M %p' as the_current_time %}/{{ the_current_time }}</p> {% upper %} <p>this is a test.</p> {% endupper %} <p>{% a_current_time '%Y-%m-%d %I:%M %p' %}</p> {% post_for_member member %} </div>

  • tag
Read More

Declaring django views like web.py views

I work a little with [web.py framework](http://webpy.org/) and I like a lot the view definition. For each view you define a class and in that class you can define two method, GET and POST. If the http request is a GET request the GET method will be called and if http request is a POST request the POST method is called. Then you can define common stuff in another method that could be called inside each method, and you have a class for each view.

  • views
  • class
  • web.py
Read More

Decorator that limits request methods

I wanted to be able to limit which types of requests a view will accept. For instance, if a view only wants to deal with GET requests. @methods(GET) def index(request): # do stuff Now, calling this view with a non-GET request will cause a 403. You can easily change this to a 404, by using a different return function: which you may wish to do with openly available sites, as a 403 indicates there is a resource present.

  • decorator
  • request
Read More

Profanity Check

Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc. This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.

  • tag
  • profanity
Read More