Login

All snippets written in Python

Snippet List

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

Highlight matched search term

sterm is the search term. text is the result search text. it will highlight every matched search term in search result. please define your own .yellow css class. use: {{search_result_var|highlight:search_term}}

  • highlighting
  • custom-template-tag
Read More

Conditional INSTALLED_APPS entry

Some INSTALLED_APPLICATIONS applications may not be critical for your website to work - for example you may only need them for development - like 'django_extensions' or 'debug_toolbar'. They needn't be installed actually for the site to work. This way you can avoid discussions with other developers if some application should be included, or is it just spam, because if they don't like it, they don't have to install it. On a production server you can leave this not installed, to avoid security concerns like a possibility to IP-spoof and see the debug toolbar.

  • configuration
  • settings
  • conditional
  • installed_apps
Read More

Multiple emails field

Model Field allowing to store multiple emails in one textual field. Emails separated by comma. All emails are validated. Works with Django admin.

  • multiple
  • email
  • validation
  • mail
  • multifield
Read More

ExcelResponse2

A function extends of Tarken's django-excel-response django-excel-response 1、djangosnippets - http://djangosnippets.org/snippets/1151/ 2、pypi - https://pypi.python.org/pypi/django-excel-response/1.0 When using Tarken's django-excel-response. We find that Chinese is messed code when we open .xls in Mac OS. As discussed in http://segmentfault.com/q/1010000000095546. We realize django-excel-response2 Based on Tarken's django-excel-response to solve this problem By adding a Param named font to set font.

  • output
  • httpresponse
  • excel
  • cvs
Read More

vary_on_user

@vary_on_user doesn't work properly if your site uses third-party cookies, since the cache key is created from *all* of the cookies in the request header. This decorator caches pages based on the user ID, so it works reliably.

  • decorator
  • vary_on_cookie
Read More
Author: TAH
  • 0
  • 0

Email Auth

Yet another authentication by email address. This one is quick and dirty as we are saving email address in both Username and Email fields. For proper way how to deal with it see https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#auth-custom-user

  • email
  • auth
Read More

Dynamic SITE_ID thread-safe

Store in SiteID a local var store for save SITE_ID thread-safe and set it with middleware. It's base on https://djangosnippets.org/snippets/1099/ but with call to local() in SiteID and using custom models for web site and domains.

  • dynamic
  • site_id
  • thread-locals
Read More
Author: jhg
  • 1
  • 0

Read-only Model Form Base Class

The simplest way of displaying a "details" table about any model, is to show a ModelFrom with all fields readonly or (selects) disabled. Also, the labels are preferably translatable, not just capitalized names of the column tables in your models. So the constructor translates the field labels as well.

  • form
  • readonly
Read More

File Mimetype Validator (Using python-magic)

This validator works well with FileField form fields and can validate that an uploaded file has an acceptable mimetype. Place this snippet in your app's `validators.py`. Requirements: This snippet uses [python-magic](https://github.com/ahupp/python-magic). To install: pip install python-magic Usage (in forms.py): from validators import MimetypeValidator class MyForm(forms.Form): file = forms.FileField( allow_empty_file=False, validators=[MimetypeValidator('application/pdf')], help_text="Upload a PDF file" )

  • validation
  • upload
  • magic
  • mimetype
  • FileField
Read More

JSON serializer supporting natural primary keys

Copy this file into `your_app/serializer.py`, and add this to your settings: SERIALIZATION_MODULES = { 'json': 'your_app.serializer', } Now you can dump your models with the classical `dumpdata -n` command and load it with `loaddata` and get support for natural primary keys and not only with foreign keys and many to many fields.

  • serializer
Read More

2955 snippets posted so far.