Uses JSMin. Python version available from
[http://www.crockford.com/javascript/jsmin.py.txt](http://www.crockford.com/javascript/jsmin.py.txt)
Provides template tags to minify JavaScript on the fly.
`{% minifyjs %}[code]{% endminifyjs %}`
A django admin command that takes a fixture and makes the target database the same as that fixture, deleting objects that in the database but not in the fixture, updating objects that are different in the database, and inserting missing ones.
Place this code in your_app/management/commands/syncdata.py
You will need to use manage.py (not django-admin.py) for Django to recognise custom commands (see http://www.djangoproject.com/documentation/django-admin/#customized-actions).
This snippet is the 'loaddata' command with this patch applied: http://code.djangoproject.com/ticket/7159 (with minor tweaks).
The intention is that 'dumpdata' on system A followed by 'syncdata' on system B is equivalent to a database copy from A to B. The database structure in A and B must match.
Add the snippet to your settings.py. If you have a settings_local.py it will load that one. Can be used in development environments where you might have different settings for your dev sandbox. You should exclude settings_local.py from SVN.
By Rudy and Ed Menendez
I will change a model form widget attribute without define the complete field. Because many "meta" information are defined in the model (e.g. the help_text) and i don't want to repeat this.
I found a solution: Add/change the widget attribute in the __init__, see example code.
I needed to be able to synchronize my LDAP users and groups to the Django database. This may not be as efficient as some might like but it works like a charm. It returns a list of messages that I pipe into request.user.messages in my template.
This tag allow you to use C-like switch tag in your templates.
It is useful for sequencial and repetitive `{% ifequal %}` tags.
To install it in your project, you just need to follow [these instructions](http://www.djangoproject.com/documentation/templates_python/#extending-the-template-system)
How to make this work:
* Put the code into your settings.py (or even better, local_settings.py if you have one)
* Put 3rd party apps you'd like to use into the directories specified in APP_DIRS.
* Place 'appname', into INSTALLED_APPS.
Just a little trick I use to keep from having to install apps via setup.py; I can just stick apps into arbitrary locations and use them. I also do this to keep single copies of 3rd party apps I use in multiple projects (but again without installing to the global python path). No idea if this is bad practice or anything, but I find it useful.
Modify a query string on a url. The comments in the code should explain sufficiently. String_to_dict, and string_to_list are also useful for templatetags that require variable arguments.
Usage: (if you save it as pigmentation.py as I did)
{% load pigmentation %}
{% autoescape off %}
{{ somevariable|pygmentize }}
{% endautoescape %}
There already a few of this code around, but this one is pretty clean, and includes css. It also works in both the development server and Dreamhost (python2.4 in my django config) without any unicode problems.
The Django JSON encoder already extends the `simplejson` encoder a little; this extends it more and gives an example of how to go about further extension.
Hopefully `newserializers` (see the community aggregator today) will supercede this, but until then, it's useful.
Sometimes we need divide forms in fieldsets, but this make us declare all fields in HTML template manually.
This class is to help you to do this by a easy way.
**How to use**
First, download this file as name "sectioned_form.py"
Later, turn your form inherited from the class **SectionedForm**, override method "_html_output" and declare fieldsets and fieldset_template attribute, like below:
from sectioned_form import SectionedForm
class MyForm(forms.ModelForm, SectionedForm):
fieldsets = (
(None, ('name','age','date')),
(_('Documents'), ('number','doc_id')),
)
fieldset_template = "<h2>%s</h2>"
def _html_output(self, *args, **kwargs):
return SectionedForm._html_output(self, *args, **kwargs)
This is an awesome script! The original can be found here: http://www.djangosnippets.org/snippets/633/
I had to make a couple minor changes to get it working for me, so I thought I would share the love...
This widget will render a chained select menu powered by JavaScript to make it easier to identify foreign keys. This widget includes danjak's form decorator (http://www.djangosnippets.org/snippets/59/), and Xin Yang's chained select javascript functions (http://www.yxscripts.com/).
I developed this to be used with an IT inventory system. See screenshot here: http://bayimg.com/cAjAGAabN
The models are laid out that location -> area -> room. But the __str__ of area and room did not include unique fields, so the built-in single select box that django uses for ForeignKey's did not work for me.
A few notes:
1: I will not be maintaining this, I am only putting it out here in case it helps others.
2: The chained select menus will only be available to the first form on the page. Reason being: the template names the form, not the django backend. So, I had to reference the form in javascript as document.forms[0].
3: Due to the javascript processing, the chain select menu will not show current values other than the default specified in the javascript. Thus, form_for_instance and a dict of values passed to form_for_model will not pre-set the chained select.
4: The rendered selects are put into a vertical table. No other layout is supported.
5: The select field names for upper-leveled options are "chain_to_[destination_field_name]__[current_selects_model_name].
6: The select name for the destination option is the name that django sends internally, which is usually the field name. The value of each option in the select is the primary key associated with that object.
7: I tried to get this built in to the native form_for_model helper function for use with the default admin site, but failed miserably.
How to get it working (quick version):
1: Define your models
2: From your view, import the form_decorator and ChainSelectWidget (I put them in CustomWidgets.py and made sure it was in the path).
3: Build arguments for the form_decorator eg:
widget_overwrite=dict(field=ChainSelectWidget(order=[(top, 'order_field'), (next, 'order_field'), (field, 'order_field)]
4: Send arguments to form_decorator eg:
callback = form_decorator(widgets=widget_overwrite)
5: Build modified form eg:
mod_formclass = form_for_model(field, formfield_callback=callback)
6: Instance the modified form eg:
instanced_form = mod_formclass()
7: Send instanced form to the templating engine
8: From the template, import the chainedselects function file (replace [] with <>) eg:
[head][script language="javascript" src="path/to/chainedselects.js"][/script]
9: Display the form object as you normally would.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.