Add model form field meta data in a DRY way
Change field meta data or add a html attribute to a ModelForm field in a DRY way.
- field
- widget
- modelform
- attrs
Change field meta data or add a html attribute to a ModelForm field in a DRY way.
This is heavily inspired by [http://code.google.com/p/smorgasbord/](http://code.google.com/p/smorgasbord/). But that couldn't reuse an existing jinja2 Environment, nor set filters on the Environment it created. This code assumes that you have `env` declared previously in the file as your Jinja2 Environment instance. In `settings.py`, you should set KEEP_DJANGO_TEMPLATES = ( '/django/contrib/', ) so that your Django admin still works. You can also set any other places that you do want to use Django templates there.
Simple filter that truncates string to specific number of letters. Example usage in template: `{{ myvariable|truncatestring:20 }}` if myvariable is "That is my long string", the result will be: "That is my long s...". Put the code into templatetags/.
This is a simple modification to the standard transaction.commit_on_success decorator that is aware of existing transactions. If a managed transaction is already active then the wrapped function is called directly, assuming the active transaction will clean up as appropriate. Otherwise, a standard commit_on_success is performed. I'm using this to wrap the save() method of models that manipulate other related models on save, e.g: @nested_commit_on_success def save(self): super(MyClass,self).save() for m in other_models: self.fix_up_other_model(m) m.save()
Supported MAC formats: aa:bb:cc:dd:ee:ff, separator : or - aabbccddeeff both lower case and upper case
Sometimes when a Django site's authentication backend goes down, a login will fail with a 500 error. This has happened to me when using an LDAP backend for authentication. A glitch with the settings, or ldap temporarily disappearing can make logins flake out for a short period of time. That's fine, but when a 500 error occurs it e-mails detailed information about the error to the ADMINS. We like this behavior for most errors, but it is quite frustrating when it is a login form with a password as part of a POST. If it is one of us who gets our password e-mailed out, it's even more frustrating. It hits a mailing list first, and goes to the archives to be stored in plain text. It goes to several e-mail inboxes, some of which are not local inboxes. I decided that enough was enough. Drop this middleware in, and it will change a "password" field in the POST to twenty asterisks. This was the default way to display other sensitive settings on the DEBUG page, so I figured I'd be consistant with that. This snippet is distributed under the GPLv3 License http://www.gnu.org/licenses/gpl-3.0-standalone.html
sorry, this is a duplicate post, original is here: http://www.djangosnippets.org/snippets/690/ no way to delete snippets
This small app can display messages to users after they login and before they get to the normal landing page. This can be useful for displaying maintenance notices, information on new features, or a one-day-sale on shoes. To redirect to the MOTD view after login, change: `<input type="hidden" name="next" value="{{ next }}" />` to: `<input type="hidden" name="next" value="{% url django_motd.views.motd %}?next={{ next }}" />` in your login.html template.
This is a skeleton framework of a unittest for an app which will write out a fixture of the test database once the test has been done. I run this once for all apps, but you can limit which apps get serialized by modifying the self.apps value from get_apps (all apps) to a list of only certain apps. This script by default assumes that you have a SVN_DIR setting which points to the current working subversion directory, with a subdirectory of fixtures where it places `tests.json` upon completion. You may change this location as well. After running `python manage test` you can run `python manage loaddata fixtures/tests.json` to load in to the real database all of the test database fixtures. Feel free to edit at will, let me know of any changes that are helpful, and dont forget to fill in the `...`s
This recipe uses a modified version of Robin Dunn's fcgi.py module that adapts fcgi to wsgi and lets you run Django under mod_fcgid. One good thing about mod_fcgid is that it does all process management for you, which makes this setup quite straightforward. Also, since Robin's module works both in a cgi and fcgi context, switching a django site between cgi and fastcgi is a one-liner in the apache config, without any changes to python code or django config. CGI may be handy for development, since it loads all code (including changed code) on every request, yet lets you work in an environment that resembles production. Apache configuration examples are found in the comment at the beginning of the python module.
An example of how to modify the admin user creation form to assign an unusable password to externally authenticated users when they are created. This code is more intimate with the django.contrib.auth classes than I'd like, but it should be fairly straightforward to maintain should the relevant django.contrib.auth classes change.
This decorator allows you to wrap class methods or module functions and synchronize access to them. It maintains a dict of locks with a key for each unique name of combined module name and method name. (Implementing class name is lost in decorator? Otherwise it would have class name too if available.) Effectively it functions as a class level method synchronizer, with the lock handling completely hidden from the wrapped function.
**Adapted from** [CountryField](http://www.djangosnippets.org/snippets/494/) - **Initial thanks to marinho** Uses the UN country list listed in the source - this provides the 3 character ISO country code. Ordered by display value and not country code. Just place anywhere you like and import CountryField to use. `country = CountryField(verbose_name="Country", help_text="The registrant's country of residence.")`
I recently had to write a custom template tag which checks to see if a value exists in a list variable from within a Django template. This was my first ever attempt at writing a template tag so any feedback would be appreciated.
This is standart phone model.
3110 snippets posted so far.