Login

Most bookmarked snippets

Snippet List

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

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

Django Settings Splitter & Local Settings loader

Everybody know about long spagetty-style settings file for django :-) I tried to find any cool settings loader, but have no luck.I created this one myself. Ok, we forgetting about `settings.py` and creating module settings (folder named settings with file `__init__.py`). This `__init__.py` file have preloader for modules placed in settings folder and `../settings_local.py` (if exists) at the end. settings_local is awesome tool, when you use any VCS like git and have settings in vcs, but for example you have different database connection settings. You can change this settings in settings_local. Settings splitter have variable moduleweights. This variable declares weights for selected modules to allow loader sort modules by priority and use already defined settings in each other loaded module. You can define your custom modules and weights there. Ok, now few examples. settings/env.py import os # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ DEBUG = not 'http/ask.helldude.ru/' in os.path.realpath(__file__) settings/assets.py # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ import os import sys settings = sys.modules['project.settings'] STATIC_URL = '/static/' STATIC_ROOT = os.path.join(settings.BASE_DIR, 'static') STATICFILES_DIRS = (os.path.join(settings.BASE_DIR, "project/static"),) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.CompressorFinder', ) settings_local.py: import sys settings = sys.modules['project.settings'] print 'I WAS LOADED KHA KHA KHA' if settings.DEBUG: print 'In debug mode' You can see amazing 'hack' there :-) I use already defined settings via sys.modules['project.settings'] (where project is common folder for my project applications). I hope you like this small lifehack for django settings! rudude.

  • django
  • settings
  • local
Read More

3110 snippets posted so far.