Login

All snippets

Snippet List

django-constance generic view

For several projects I am using generic views instead of django-admin, I needed a generic view for constance instead of using their django-admin based app.

  • django-constance
Read More

django form template with bootstrap

## required * `{% load trans%}`before using this snippets * Add this [template filter](https://djangosnippets.org/snippets/2253/) to your custom templatetags and load it before using this snippets * Bootstrap framework

  • template
  • form
Read More

django form template with bootstrap

## required * `{% load trans%}`before using this snippets * Add this [template filter](https://djangosnippets.org/snippets/2253/) to your custom templatetags and load it before using this snippets * Bootstrap framework

Read More

Decorate class-based views with regular decorators

Decorating a whole view involves overriding the dispatch method so you can decorate it, even if all you do is a passthrough. This creates a class decorator for decorating CBVs. For example, if you want to make a view require a login: @decorate_dispatch(login_required) class MyCBV(TemplateView): ...

Read More

Alias Field

A "fake" model field which is an alias to another underlying database field. Mirrors everything including queryset lookups, instance attributes, even if the field has been used on the model already (this isn't possible just by using db_column).

Read More
Author: s29
  • 0
  • 0

Readonly Select Widget Replacement

This is for situations where a ModelForm is needed on an existing object, but we want to disable ForeignKey widgets, and only use them as a display. Usage: save the code in the top half of the example above into a file somewhere in your django project, in this example we will call it customwidget.py The second half shows an example usage, but setting the widget and queryset in the __init__ method of the form. If the queryset returned by the ForeignKey model (Example.objects.all() ) is small, you can omit the __init__ code.

Read More

geodjango Metric Buffer

GEOSGeometry.buffer() accepts a distance argument which is in the unit of the coordinate reference system of the geometry. It is often necessary, however, to specify the buffer size in a more down-to-earth coordinate system, for example meters. with_metric_buffer(geom, buf_size) implements this functionality.

Read More

decorator for runsnake

1. Add this decorator to whatever function: @profile('/tmp/0123test.prof') def test_function(): pass 2. If it's a django view function, let it run once. 3. Use "runsnake" to open the ".prof" file. Sweet.

Read More

Previewing Django templates in a browser, without even creating a Django project

**preview_template.py** allows you to test a Django template located in the current working directory (first argument). The template is rendered with the given context (second argument, *optional*), and the result is immediately piped into the browser with the [bcat](http://rtomayko.github.io/bcat/) utility. **Usage:** python preview_template.py [template name] [context] **Example:** python preview_template.py template.html "{'username': 'Његош'}"

Read More

3109 snippets posted so far.