Login

Top-rated snippets

Snippet List

template filter to include protocol and domain in absolute urls

I created this template filter to be able to use get_absolute_url in an email template. Save the code into /templatetags/navigation.py Use like this: {% load navigation %} {{ instance.get_absolute_url|siteabsoluteurl:request }}

  • template
  • filter
  • templatetag
  • template-filter
  • url
  • template-filters
  • get_absolute_url
  • templatefilter
  • absolute
Read More

Generic compact list_filter with counts

Based on [#1879](http://djangosnippets.org/snippets/1879/) and [#2356](http://djangosnippets.org/snippets/2356/) Works in Django 1.3 Hopefully it's generic enough to implement a compact (sparse) version of whatever custom filter you need.

  • admin-interface
  • compact
  • list_filter
Read More

Fetching list of SQL queries executed so far for all requests

These snippets together give you the ability to view all Django SQL queries executed across all incoming requests by visiting a particular URL (`/profiling` in the example). This is useful when developing with the Django test server. This is useful if most of the incoming requests are AJAX requests, because in such cases the debug toolbar will not be able to show which queries got executed. The `SqlProfilingMiddleware` class is the key one. At the end of every incoming request it appends the executed SQL queries to a static class member list. Any information request profiling information can be added in this way. The snippet does not add any security around viewing such information. This was done just to keep the code simple. But when implementing this you will definitely want to restrict access to this URL to only people allowed to view such information.

  • sql
  • debugging
  • profiling
Read More

Django Sudo

Staff can log in as a user, from a url to help with customer support or debugging.

  • admin
  • user
  • login
  • staff
  • sudo
Read More

Paginator for PostgreSQL

Use this paginator to make admin pages load more quickly for large tables when using PostgreSQL. It uses the reltuples statistic instead of counting the rows when there is no where clause. To use this code, add the following in your admin: `class BigTableAdmin(admin.ModelAdmin): paginator = LargeTablePaginator def get_changelist(self, request, **kwargs): return LargeTableChangeList `

  • paginator
  • postgres
  • reltuples
Read More

When you can't return a response object, throw it

There are cases when rendering had already started, but you have to return Your response nevertheless. A good example is when you have a django-cms plugin and a form in it. You want to redirect after the form was processed, but normally you can't do it. More information here: https://github.com/divio/django-cms/issues/79 http://groups.google.com/group/django-cms/browse_thread/thread/79ab6080c80bbcb5?pli=1

  • middleware
  • redirect
  • django-cms
Read More

View mixin and utils to generate PDF documents from html using xhtml2pdf

View mixin and utils to generate PDF documents from html using *xhtml2pdf*. The most interesting thing here is *PDFTemplateResponseMixin*. Adding this mixin to class based views allows automatic pdf generation using the view context and a customized template. There is also the lower level function *render_to_pdf*, similar to what can be seen in [snippet 659](http://djangosnippets.org/snippets/659/). See the docstrings for a detailed explanation.

  • pdf
  • mixin
  • class-based-views
Read More

Using reverse with success_url in class based generic views

If you did tried to use `reverse` function to set `success_url` in class based generic views and you have got an exception, this helper function may help. Put this snipped in some file, for example utils.py and import this function.

  • reverse
  • class-based-generic-view
  • success_url
Read More

GeoJSON Serializer for GeoDjango (gis)

**Please use the updated version http://djangosnippets.org/snippets/2596/** Unfortunately the built in Django JSON serialzer encodes GeoDjango GeometyrField as simple text. This snippet extends django's serializer and adds support for GeoJson format. Built in JSON serializer output: [{"pk": 1, ... "geopoint": "POINT (-76.5060419999999937 44.2337040000000030)" ... }] GeoJSON serializer ouput: [{"pk": 1, ... "geopoint": {"type": "Point", "coordinates": [-76.503296000000006, 44.230956999999997]}}]

  • GeoJson
  • Serializer
Read More

Simple DRY Tabs using Django 1.3

I was just playing around with Django 1.3 and came across the new 'with' addition to the include tag. I thought this was a super elegant way to implement a DRY tab menu without having to move menu code into the views. Enjoy!

  • menu
  • dry
  • tabs
  • menus
  • tabbed
  • tab
Read More

AjaxForm Base Classes

These base form classes add a method to return error messages as HTML encoded as JSON from Django, optionally passing in an argument to strip tags out. The method can be called in your view after checking that your form is valid. There is a ModelForm and Form class to use depending on your needs. The sample jQuery function will take the errors returned as json, loop over the errors and insert the error after each field. If you're using a form prefix, you'll need to add a hidden field to hold the value for the prefix. The `__all__` error(s), which are not bound to a field are appended to the end of the form, which you could easily reposition. Happy coding!

  • ajax
  • json
  • form
Read More

login_required decorator that doesn't redirect

A login_required decorator that wraps the login view instead of redirecting to it. This prevents your site from leaking login information with HTTP status codes as explained [here](https://grepular.com/Abusing_HTTP_Status_Codes_to_Expose_Private_Information). This is the way Django's admin is protected, the difference being that it checks for is_active and is_staff instead of is_authenticated. With this decorators, users directly see a login form (no redirect), post it to LOGIN_URL and are redirected to the page they first tried to see.

  • redirect
  • login_required
Read More

Class based generic views that automatically check permissions

Simple wrappers around the new class-based generic views (introduced in Django 1.3) that also check permissions. **Example Usage** *(views.py)*: from mymodule import RestrictedListView, RestrictedUpdateView class MyListView(RestrictedListView): model = MyModel class MyUpdateView(RestrictedUpdateView): model = MyModel and so on for Create and Delete.

  • generic-views
  • permissions
  • class-based
Read More

3110 snippets posted so far.