MultiForm and MultiModelForm
Based on a PrefixDict class I wrote and thus very lean. Lacks a little documentation, though
class MyMultiForm(ModelMultiForm):
class Meta:
localized_fields = '__all__'
form_classes = OrderedDict((
('form1', Form1),
('form2', Form2),
))
Subfields are named `form-name` `prefix_sep` `subfield-name`. `prefix_sep` defaults to `-`. For access in templates, use `form.varfields`, which uses `var_prefix_sep` (default: `_`), instead.
multiform.varfields()['form1_field1']
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*.
**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.
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.
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.
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()
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}}
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.
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.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.