Login

Most bookmarked snippets

Snippet List

Middleware for fixing Microsoft Office (Word, Excel, Powerpoint) hyperlinks to views requiring authentication

Hyperlinks to views requiring authentication in Microsoft Office (Word, Excel, Powerpoint) can fail based on how Office handles rendering. This middleware sends a refresh return to the client, which will allow the page to be opened as normal, instead of the "Unable to open ... Cannot download the information you requested." This is a port from the ruby middleware project [fix microsoft links](https://github.com/spilliton/fix_microsoft_links). To enable, add to your [middleware stack](https://docs.djangoproject.com/en/dev/topics/http/middleware/) in the django project settings.

  • middleware
  • office
  • word
  • excel
  • powerpoint
Read More

Ellipsis paginator decorator with first and last two items

Use this template tag to get a paginator showing the first and last two pages w/ adjacent pages using ellipsis. The `page` parameter is a page of a `Paginator` (typically the first but you can use whichever you want). In case of 50 pages, while being on the 40th, it'll give you the following iterable of `int`s (with `settings.PAGINATOR_ADJACENT_PAGES = 2`): `(1, 2,    38, 39, 40, 41, 42,    49, 50) ` You get the idea.

  • decorator
  • paginator
  • ellipsis
Read More

Smart wrapping Django admin's delete_selected

The idea here is to wrap the original `delete_selected` functionality in a way that I shouldn't have to reimplement the templates (confirmation/error response) serving, just extend the original. What this code does, it wraps the queryset's delete function with a closure, so when it really gets called (after the confirmation), it executes the extra functionality you wish to. After looking at the original code, this seemed to be the most efficient way of doing it.

  • admin
  • wrap
  • delete_selected
Read More

Make HTML5 appcache manifest slice work with django-compress compressed files

I needed to make appcache for my application which used django-compress for JS and CSS compression. This is, how I solved the problem with putting compressed files into the manifest. I went for offline compression (with `COMPRESS_OFFLINE=True`). This snippet shows code of command file (put it in `apps/cyklomapa/management/commands/compress_create_manifest.py`), which creates `compress_cache_manifest.txt` file in my templates. Then I just use `{% include "compress_cache_manifest.txt" %}` in my appcache template.

  • html5
  • django-compress
  • appcache
Read More

Mutiple forms on Form wizard

Wraps many Form subclases in order to get a form wizard to treat them as one. Many Forms will use one step. **Example:** `class LanguageVerifiedHumanForm(MultipleForms): base_forms = [ ('language_form', LanguageForm), ('verified_human_form', VerifiedHumanForm) ]` `class NewGuideWizard(SessionWizardView): form_list = [ ('guide_form', GuideForm), ('multi_form', LanguageVerifiedHumanForm), ]`

  • multiple
  • forms
  • wizard
  • multi
  • form-wizard
Read More

Dynamic formset without javascript

If using javascript is not an option, you can use something like this code to have a variable number of subforms. This code uses crispy-forms, but it is totally dispensable.

  • formset
  • dynamic-formset
  • formset_factory
Read More

Reusable field forms using crispy-forms

If you need to use some source to construct your form (maybe some database info or some user input), or you use the same fields with the same functionality in various forms and need to write the same piece of code for each of them, then you are looking in the right place. With this snippet you won't have those issues any more. :P This snippet present a way to wean the fields from the form. The code is made using crispy-forms, but the same idea can be applied without using it. Best luck!

  • forms
  • filters
  • reuse
  • reusable
  • form-field
  • crispy-forms
Read More

Django filter for shrinking [big] numbers

Simple filter that shrinks [big] numbers sufixing "M" for numbers bigger than million, or "K" for numbers bigger than thousand. It does a division over the number before converting to string so rounding is properly done. Examples: `{{ 123456|shrink_num }} >> 123.6K` `{{ 1234567|shrink_num }} >> 1.2M`

  • filter
  • numbers
  • kilo
  • mega
Read More

Datetime adjuster for Django Templates

The filter is **specific to datetime objects** and no allowance has been made to convert strings or epoch times. Also **no type checking** is performed so misuse will result in an error. To use include the above snippet in a file called templatetags/customfilters.py or append to existing filters file then load: `{% load customfilters %}` in your template, then to use it: `{{ dateobject|adjust:"months=1"|date:"Y/m/d" }}` To push the dateobject forward by a month, or: `{{ dateobject|adjust:"weeks=-1"|date:"Y/m/d" }}` To push the dateobject back a week, or: `{{ dateobject|adjust:"years=1, months=2"|date:"Y/m/d" }}` To push the dateobject forward by a year and 2 months

  • template
  • filter
  • datetime
  • calculation
  • adjust
  • relativedelta
Read More

Remove a clause from a queryset

I want to create Mixins for QuerySet objects that will by default filter out certain records (e.g. filter out "deleted" records, or filter out "unapproved" records, etc). I'd like those to be separate independent mixins. So in each of those, I override all() to filter out deleted or unapproved, etc. But, I also want to offer a method in the queryset to remove those filters or remove some part of those filters. That's where this code comes in. After some examination of how QuerySets work, this seemed like the simplest method for "undoing" some filter in a queryset

  • hack
  • orm
  • manager
  • mixin
  • queryset
Read More

ReadOnlyFieldsMixin to Form, ModelForm and Views (helper function)

### Usage: class Foo(models.Model): description = models.TextField() number = models.IntegerField() class FooOnlyDescriptionIsReadOnly(ReadOnlyFieldsMixin, forms.ModelForm): readonly_fields = ('description', ) class Meta: model = Foo fields = '__all__' class FooAllFieldsIsReadOnly(ReadOnlyFieldsMixin, forms.ModelForm): class Meta: model = Foo fields = '__all__' ### or use the function class FooForm(forms.ModelForm): class Meta: model = Foo fields = '__all__' ReadOnlyFooForm = new_readonly_form_class(FooForm, readonly_fields=('description', ))

  • readonly-form
  • ReadOnlyFieldsMixin
Read More

3110 snippets posted so far.