Login

All snippets written in Python

Snippet List

Custom Admin Redirects

This little bit of code will let you reference parts of the admin but lets you control where it returns to. For instance, I have a list of objects in the admin and i want to have a delete link for each object. I don't want them to return to the changelist after a delete however, i want them to return to my list. cheers

  • admin
  • redirect
Read More

Filter to adjust forloop.counter across pages in a paginated view

**Update:** Never mind. See [dc's comment](http://www.djangosnippets.org/snippets/1391/#c1763) below for a much easier way to do this. I recently had to write a template for a paginated view which displayed a serial number for each `object` in the `object_list`. I normally use `forloop.counter` for general purpose serial numbers. However this did not work with paginated views as the counter gets reset in each page. This caused the serial numbers to go from 1 to #-of-results-in-the-page and then repeat. **Assumptions:** The `adjust_for_pagination` filter adjusts the value of `forloop.counter` based on the current page. `Page` and `is_paginated` variables are expected to be present in the context. These should respectively denote the current page number (1 based) and if the results are paginated. `RESULTS_PER_PAGE` is currently taken from the settings file. I couldn't think of a way to pass this value also from the template.

  • filters
  • pagination
  • forloop.counter
Read More

django-noserun for testing

I wanted to use Nose with Django so I came up with this. `TEST_RUNNER = 'noserun.run_tests'` in settings.py It does not do setup/teardown implicitly between test methods, you need to call *nosetest.test.flush()* and *nosetest.test.loaddata()* manually if you want that. Enables the method names *setup* and *teardown* The environment variable *NOSE_COVER* runs coverage tests and *NO_DROPDB* preserves the test db.

  • testing
  • test
  • nose
Read More
Author: mjt
  • 1
  • 2

Paginator Tag for 1.x

This is a [Paginator Tag](http://www.djangosnippets.org/snippets/73/) for 1.x. Since the context is less overfull, the template, paginator.html, needs more logic. Put the tag in your templatetags and the template at the root of a template-directory. The tag will work out of the box in a generic view, other views must provide `is_paginated` set to True, `page_obj`, and `paginator`. You can get the `object_list` from the `page_obj`: `page_obj.object_list`. See [the pagination documentation](http://docs.djangoproject.com/en/1.0/topics/pagination/).

  • tag
  • paginator
Read More
Author: HM
  • 1
  • 4

request_logger

Simple logger, stores all query parameter and post parameters for each query.

  • debugging
  • logger
Read More

function tracing decorator

This is a decorator that logs function arguments, return object and time taken for execution as well as any exception that might have been raised by the function. Useful for debug logging.

  • decorator
  • debugging
  • tracing
Read More

Globs for INTERNAL_IPS

Allows you to include globs of IP addresses in your INTERNAL_IPS. It's shell-style glob syntax (the [fnmatch module](http://docs.python.org/library/fnmatch.html)). This should be helpful with the [Debug Toolbar](http://github.com/robhudson/django-debug-toolbar/tree/master), among other things. Like [#1362](http://www.djangosnippets.org/snippets/1362/), but with no external dependencies.

  • settings
  • debug
  • ip-addresses
  • internal-ips
Read More

iPhone Redirect Middleware

I didn't really like the current state of iPhone/Mobile redirect middleware mainly because I wanted something that was closer to twitters use case. So I came up with this. I don't think it a great snippet and I will probably fix it in the near future. But it works.

  • middleware
  • session
  • redirect
  • iphone
Read More

Resolve URLs to view name

This snippet suplies a resolve_to_name function that takes in a path and resolves it to a view name or view function name (given that the path is actually defined in your urlconf). Example: === urlconf ==== urlpatterns = patterns('' (r'/some/url', 'app.views.view'), (r'/some/other/url', 'app.views.other.view', {}, 'this_is_a_named_view'), ) === example usage in interpreter === >>> from some.where import resolve_to_name >>> print resolve_to_name('/some/url') 'app.views.view' >>> print resolve_to_name('/some/other/url') 'this_is_a_named_view'

  • view
  • url
  • resolve
  • name
Read More

ChoiceField with widget in choice

This code creates a widget that we used to generate a list of radiobuttons as follows: * Radio button 1 --Widget__1 * Radio button 2 --Widget__2 * Radio button 3 --Widget__3 How to use it is: `ImagenForm class (forms.ModelForm): widget1 = forms.BooleanField () widget2 = forms.TextInput widget3 = forms.ModelChoiceField (queryset = Modelo.objects.all ()) widget4 = forms.FileInput optConListas = ChoiceWithOtherField (choices = [(2, 'widget1' widget1), (3, 'widget2' widget3.widget), (4, "widget2" widget2), (5, 'widget4' widget4)])`

  • django
  • choice
  • choicefield
  • widget
  • alfonso
Read More

2956 snippets posted so far.