Login

All snippets written in Python

2956 snippets

Snippet List

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

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

Template filter for internal links in TextFields

Allows for in internal link markup to be saved inside markdown-formatted Textfield entries. Using the filter, the link is only looked up at display time, so if your view's URL has changed, that should automatically update with the reverse() lookup. You could tweak the regex pattern to match whatever link markup you prefer. I also use Markdown to process my description fields, so I make the link return a markdown-formatted link instead of HTML, but you could tweak that too. If you use Markdown, you'd want to put this filter first. So to display a description TextField with internal links, in the template would be something like this: `{{ entity.description|internal_links|markdown }}` (See the [Django docs on writing your own custom filters](https://docs.djangoproject.com/en/1.6/howto/custom-template-tags/#writing-custom-template-filters) for more details on writing and registering filters.) Written for [my own website](http://opticalpodcast.com), and a basic version was shared as the [answer to this Stack Overflow question](http://stackoverflow.com/a/26812762/429070).

Read More

Forcing DB on relationship manager with multiple database project

I have a master/slave SQL setup and sometimes I need to access the data immediately after write, but the data has not yet propagated to the slave yet. This forces me to require `.using('default')` every time that relationship is accessed. Ex: self.books.using('default').all().delete() Setting `objects = ForceDefaultDBManager()` on the related object removes this requirement throughout your code. This now does the same as the last example: self.books.all().delete()

Read More

Extended Seconds-to-Duration Template Tag

Extended Template Tag initially created by Dan Ward (http://d-w.me): https://djangosnippets.org/snippets/1398/ The default setting has been renamed to normal, output stays the same. If you have used the old tag with ':short' you have to remove or rename it to ':normal' for the same ouput! As an example, given the duration 84658: Short: 23:30:58 Normal (default): 23 hrs 30 mins 58 secs Long: 23 hours, 30 minutes and 58 seconds Best regards, Gregor Volkmann

Read More

log django exceptions to file

Log Django exceptions to file. Contains snippet for both WatchedFileHandler and RotatingFileHandler handlers. Configurations mentioned are separate And only works if the DEBUG = False. Use "python manage.py runserver --no-reload" If you get file in use error.

Read More