Login

All snippets

Snippet List

Encrypted Custom Model Field

Custom model field to support two ways encryption. The key is provided in the app settings.py file. It should be at least 32 chars. ** usage ** Assuming that you placed the new code into fields.py from app.fields import EncyptedField class Account(models.Model): password = EncryptedField()

  • custom-model-field
  • encrypted
Read More

One line SMTP sink server

Start simple SMTP server on localhost:25 and print to standard output all email headers and the email body. Useful for debugging outgoing mail without configuring SMTP daemon in development enviroment.

  • email
  • debug
  • smtp
  • mail
  • debugging
  • debuggingserver
Read More

Loading initial data per model at table creation (useful with migrations)

A very simple way of automatically loading data on model creation. As I am using South I wanted an automatic way of loading initial data only when a new model is create during a migration. Django provides almost everything out of the box by providing the *post_syncdb* signal (triggered also by South migrate command) and the *loaddata* command. The code will simply look for a an existing fixture named `<model>_initial.*` and invoke the *loaddata* command to try to load it Note: beware *post_syncdb* signal is deprecated since version 1.7: it has been replaced by *post_migrate*.

  • loaddata
  • migration
  • south
Read More

Admin action for a "CSV Export" with ManyToManyField

Based on [#2369](https://djangosnippets.org/snippets/2369/) Save the snippet as actions.py within your django app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: from actions import export_as_csv_action class YourModelAdmin(admin.ModelAdmin): list_display = (...) list_filter = [...] actions = [export_as_csv_action("CSV Export", fields=[...])]

  • ManyToManyField
  • Adminactions
  • ManyToManyFields
  • Export-CSV
  • Adminaction
  • CSV
  • Export
  • ManyToMany
  • Many-to-many
Read More

Virtualfish Hook

When you use Virtualfish, the virtualenv wrapper for the fish shell, you can add hooks for whenever the virtualenv is activated. This little snippet sets some important environment variables and adds the manage.py command with autocompletion so that you won't ever need to write "python ./manage.py help" ever again, but only "manage <TAB>". For this to work you need to source or paste this code in your ~/.config/fish/config.fish

  • manage
  • virtualenv
  • fish
  • virtualenvwrapper
  • virtualfish
Read More

MultiRangeField and MultiRangeFormField

**Designed to hold a list of pages and page ranges for a book/magazine index.** A custom model field (and accompanying form field) that saves comma-separated pages and page ranges in human-readable string form. Includes some clean-up code, so that you can add a new page or range at the end of an existing entry, and it will put it in numeric order and combine runs into ranges. So this: 4-33, 43, 45, 60-65, 44, 59 becomes the tidy 4-33, 43-45, 59-65 **NOTE:** If you comment out the raising of the `ValidationError` in the form field's validate() method, it will actually clean up any extraneous characters for you (which could be dangerous, but for me is usually what I want), so even this horrible mess: ;4-33, 46a fads i44 ,p45o gets cleaned to 4-33, 44-46 *This is the first custom field I've ever written for Django, so may be a little rough but seems to work fine.

  • multiple
  • stringformat
  • range
  • pages
  • index
Read More

Creating custom time entries in Django Date widget

jQuery code for making custom list on Admin page in DateTime widget. Create new js file in your static folder with this code. To use add custom js to Admin page like this: class NiceAdmin(admin.ModelAdmin): class Media: js = ('js/adminNice.js',) This code will change **all** DateTime widgets on selected page.

  • time
  • widget
  • django-admin
Read More

SSL Force Middleware

A simple way to force SSL on all pages. It's very simple at this point - the only issue I can see right now and I will address this later is if someone sends http:// in another portion of your url.

  • middleware
  • django
  • ssl
  • secure
Read More

Pretty Print Template Tag

This defines a new template tag that lets you print your rendered templates (or partial templates) in html that has been prettified by beautiful soup. It is dependent on the beautiful soup library (bs4). Not recommended for production but it is helpful for development and serving readable html. {% load whatever_template_file %}' <body> {% pretty %} <h1>Hello, world.</h1> {% endpretty %} </body>

  • beautiful-soup
  • template-tag
  • pretty-print
Read More

Automatic Folder FileField

Your MEDIA_ROOT directories are a mess? FileField save on "upload_to" directories with old/strange/temporary names decided "on the fly" and never fixed down? SmartFolderFileField is the solution! "upload_to" directory depends only on: app, model and field names. No mess, no ambiguities Obviously, in case you need a real callable for a dynamic directory name: please use it! and leave apart FixedFolderFileField Sample: from django.db import models from utilities_app.models import SmartFolderFileField class SampleModel(models.Model): sample_char_field = models.CharField(max_length=50) sample_file_field = SmartFolderFileField()

  • upload_to
  • FileField
Read More

3109 snippets posted so far.