Login

All snippets written in Python

2956 snippets

Snippet List

base class to easily expose json based web services

*JsonWebService.jsonresponse JsonWebService provides the property jsonresponse which marks the method it is applied to, and also serializes to json the response of the method. *JsonWebService.urls Scans the marked methods and build a urlpattern ready to use in urls.py

  • json
  • webservice
Read More

Simple Syslog Logging Class with Decorator

Simple logging decorator. Logs the function name along with the message. `Jul 14 16:10:42 mtzion test_func: 1 two` Define SYSLOG_FACILITY in settings.py. import syslog SYSLOG_FACILITY = syslog.LOG_LOCAL1

  • decorator
  • logging
  • syslog
Read More

Partial Tag

Lets you include another template and pass in named variables. Use like: {% partial field_template field=form.city %} If no key is specified, it will use "item" instead. You may pass in multiple variables as comma-separated key=value pairs.

  • context
  • partial
  • include
  • ssi
Read More

Parsing Tag decorator

A simple decorator to register a template.Node object as a tag, without the need to write the usual "`token.split_contents()` yatta yatta blatta blatta" function. The decorator should be called with the name of the tag, and an optional `required` named argument in case your tag requires a minimum number of arguments. Please note that all the arguments in the templatetag call will be passed to the node (except the templatetag name and the `for` and `as` keywords) in the order given. Suggestions etc are very welcome, of course =)

  • templatetag
Read More

DRY template rendering decorator update

Based fully on [snippet 1929](http://www.djangosnippets.org/snippets/1929/) **The update is:** It checks to see the view returns an instance of HttpResponse and returns that rather than trying to render it. This allows you to return something like `HttpResponseRedirect('/')`, or use a normal `render_to_response` to use a different template. *Also updates cleandict as per comment on original snippet* In this case the 'render_template' decorator assumes there is a myview.html template. this keeps things simple and you DRY. Hope it helps.

  • template
  • decorator
  • rendering
  • dry
Read More

Test if sessions are working

I wasn't sure if my setup supported sessions properly. This view helped me make sure. Usage: fill the inputs with text and make sure that these values traverse a couple of requests. If it doesn't work then maybe the session backend you've set is broken?

  • sessions
Read More

Anonymous required decorator

A decorator that allows the programmer to restrict access to some views only to non logged-in users. For instance, if an user in logged in, it should be denied access to views like /accounts/register or /accounts/login.

  • decorators
  • acl
Read More

rss news

publish tldrelative rss news exampled web.montao.com.br/li

  • rss
  • google
  • app
  • gae
  • engine
  • news
Read More

Load initial form fields from GET parameters

It is often convenient to be able to specify form field defaults via GET parameters, such as /contact-us/?reason=Sales (where "reason" is the name of a form field for which we want a default value of "Sales"). This snippet shows how to set a form's initial field values from matching GET parameters while safely ignoring unexpected parameters.

  • forms
Read More

middleware for user_passes_test

Middleware to decorate views with user_passes_test in a centralized, url-matching manner. Makes it easy to apply permissions across large sections or all of a site.

  • auth
  • permissions
  • decorators
Read More

admin filters as select boxes

This templatetag let's you output a form with select boxes instead of the ul's for filters. Uses some hacks to get the param names out of query strings. This would be a lot easier if filterspecs defined params instead of query strings (if filter tag would handle the encoding)

  • filter
  • admin
  • filterspec
Read More
Author: gzy
  • -1
  • 3

Get object/list or None

1. Function - get_obj_or_none Returns an object or a None Value 2. Function - get_list_or_none Returns a list object or None Value

  • "get_obj_or_none"
  • "get_list_or_none"
Read More

typo_comparison

This is simple validation weather a string is close enough to what we want. First param is keyword we are comparing to Second is user's input and third is tolerance level. Its very rudimentary. I have my mind fixed upon some imperfections. I am trying to make it good for human like(crazy keyboard) error.

  • comparison
  • errors
  • typo
  • validate
Read More

Limit ManyToMany fields in forms

Limit ManyToMany fields in forms. Hide the field, if only one item can be selected. e.g. For limit sites choices only to accessible sites. Also available via django-tools: http://code.google.com/p/django-tools/

  • forms
  • manytomany
Read More

match filter

A filter that re.matches a regex against a value. Useful for nav bars as follows: {% if location.path|match:"/$" %} class="current"{% endif %} For `location.path` see my [location context_processor](/snippets/1685/).

  • regex
  • match
  • nav
Read More