Login

All snippets written in Python

Snippet List

Interactive debugger and other Paste niceties

runserver with extra development tools: 1. interactive debugger that pops up automatically if an exception occurs, courtesy of [Werkzeug](http://werkzeug.pocoo.org/documentation/dev/debug.html#using-the-debugger) 2. logging of print statements directly into HTML (div float), courtesy of [Paste](http://pythonpaste.org/modules/debug.prints.html). Credits: [piranha.org.ua](http://piranha.org.ua/)

  • print
  • django
  • debugging
  • paste
  • wsgi
  • werkzeug
Read More

Pagination Template Tag

Django Pagination Template Tag that allows unlimited customization to the current Django Pagination. Automatically creates template variables that can be used to display the pagination in any format that you can desire such as Previous 1 2 3 [ 4 ] 5 6 7 Next First Previous 12 14 15 16 17 [ 18 ] 19 20 22 25 Next Last Showing 25 of 80 Results First Page 23 27 30 33 [ 36 ] 38 41 44 50 Next Last

  • pagination
  • custom-pagination
  • django-custom-pagination
  • customize-pagination-django
  • pagination-templatetag
Read More

Django Uri Param Generator

This is a simple URI Querystring generator that can be used with Django for generating URI Querystring and preserving the current Currently working to port into a template tag to allow {% urlgen page|5 %} {% urlgen page 5 %} {% urlgen page,5 %} OR {% urlgen sort name display all %} etc..

  • pagination
  • url
  • uri
  • djangourl
Read More

Render Decorator

Simple decorator to render an html template or return a json response. The view must return a dict object. Usage example: @render('page.html') def a_view(request): #do something return {'key': 'value'}

  • render
  • decorator
Read More

TemplateForm

This a mixin that can be used to render forms from predefined templates instead of using .as_table / .as_p / .as_ul

  • template
  • form
Read More

Optimized humanize naturalday filter

In Django humanize naturalday filter, you could not get the time part since a programming issue, If you apply it like: {% load humanize %} {{ response.date_submitted|naturalday:"Y/n/j H:i" }} You can get 'today', 'yesterday', 'tomorrow' or empty, but you can not get such as '2009/9/10 11:56' So I change it like above code. Now you can get 'today', 'yesterday', 'tomorrow' or '2009/9/10 11:56'

  • original-bug-fixes
Read More
Author: c2j
  • 1
  • 3

create and authenticate an anonymous user

If you want anonymous visitors to your site, or parts of your site to be authenticated as real users so that you can treat them as such in your views and models, use this snippet. Add the above AuthenticationBackendAnonymous middleware into AUTHENTICATION_BACKENDS in your settings.py and use the snippet anonymous_or_real(request) in your views, which returns a user. Comment out the bit where it creates a profile if you are not using profiles.

  • authentication
  • anonymous
Read More

Conditional Caching

This trick is for caching a view only if it passes some condition, for example, if there are more than zero items in a list. The same methodology could be used for conditional applying of other decorators.

  • views
  • cache
  • decorators
Read More

HTTP Authorization Middleware/Decorator

Use HTTP Authorization to log in to django site. If you use the FORCE_HTTP_AUTH=True in your settings.py, then ONLY Http Auth will be used, if you don't then either http auth or django's session-based auth will be used. This assumes that the regular auth middleware is already installed. If you provide a HTTP_AUTH_REALM in your settings, that will be used as the realm for the challenge. Having both a decorator and a middleware means that for site-wide http auth, you only need to specify it once, but the same code can be used as a decorator if you want part of your site protected using htty basic auth, and the other bits freely visible. Of course, since this is basic auth, then you need to make sure your site is running under SSL (HTTPS), else your users passwords are effectively transmitted in the clear.

  • middleware
  • decorator
  • http-auth
  • basic-auth
Read More

Technical 500 by group membership

Based loosely on [Eric's middleware](http://ericholscher.com/blog/2009/sep/5/debugging-django-production-revisited/), this middleware will show the technical 500 page (which you'd get if DEBUG == True) to any user who is (1) superuser and (2) a member of the settings.TECHNICAL_500_GROUP_NAME group. (If no setting exists, 'Technical Errors' is the presumed group name. I agreed with the comments that caching should be unnecessary given the (presumptive) edge case of exception + superuser. Assuming you don't have tons of superusers, this code is a good bit simpler.

  • admin
  • user
  • auth
  • debugging
  • 500
Read More

2956 snippets posted so far.