Prepend span with text, image or other data to any django widget so bootstrap can format it like in [here](http://twitter.github.com/bootstrap/base-css.html#forms) (scroll to "Extending form controls" section)
Example usage:
`
example_field = CharField(
max_length=255, min_length=1,
label='Label', required=False,
widget=PrependWidget(base_widget=TextInput, data='@')
)
`
@cache(60)
def heavy_computation():
for x in xrange(10000000):
pass # just waste some time`
The result of `heavy_computation` calls will be cached for 60 seconds. This cache decorator does not use the Django cache backend and uses the local memory instead.
If you have long words (no spaces) that are so long that it's messing up your design, add a 0-width space in the word every X chars.
Usage:
Step 1. Inside your app's directory, create dir called 'templatetags'. In that directory, create a .py file (say 'app_extras.py'). Make sure you make this a python module, make an empty the init .py (with the 2 underscores on each side).
Step 2. Inside template (make sure app is on INSTALLED_APPS list in settings.py):
{% load app_extras %}
Step 3. Enjoy!
{{ some_long_word_with_no_breaks|zerowidthspace_separator:25 }}
This snippit can be used to generate unique file names for uploads. `upload_to` allows a callable, but only provides two arguments: `instance` and `filename`. In order to prevent dumping all of the files for a model into one directory, this utilizes the `partial` decorator from `functools`, which essentially allows adding an extra argument to the function. The returned path will be of the form: `[model name]/[field_name]/[random hash].[filename extension]`
Based on [snippet #1212](http://djangosnippets.org/snippets/1212/) along with it's comments.
Replaced the for loop with translate.
example usage:
from django.core.cache import cache
from mysnippet import cache_key_clean
expensive_func = lambda x: 'x{0}x'.format(x)
input_string = "I wanted a nice value."
key = cache_key_clean(input_string)
result = cache.get(key)
if result is None:
result = expensive_func(input_string)
cache.set(key, result)
This one takes a template path as an argument. Return dictionary with template values from your view. It's simple as:
@render_to_template('posts/post_list.html')
def api_get_all(request):
return {'test': 'testing!'}
Validate Ukraine telephone numbers in popular formats:
+380 XX XXX-XX-XX
0XX-XXX-XX-XX
(0XX) XXX-XX-XX
This snippet fixes the errors found in
http://djangosnippets.org/snippets/2579/
Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890".
Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))
Can reject multiple number types so you can tune the form input to accept only landline or only mobile, or whatever combination you want.
Corrects the errors found in http://djangosnippets.org/snippets/1207/ and adds extra functionality and detail to the code found at http://djangosnippets.org/snippets/2809/
In particular, this version rejects individual invalid area codes and caters for area codes with mixed-length numbering in fine-grained detail.
**Uses info from:** [here](http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers)
Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890"
Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))
Corrects the errors found in http://djangosnippets.org/snippets/1207/
Add a dummy text for your tests
Copy/Paste script into management app **content/management/commands/importcontent.py**
**Usage:** python manage.py importcontent 40
Add a dummy contact for your tests
Copy/Paste script into management app **content/management/commands/importcontact.py**
**Usage:** python manage.py importcontact
The faster you fail the faster you reach success. This management command runs tests within the django environment, but without a test database, hence the word "UNSAFE". It only runs unittests for a single application, which are not subclasses of django.test.TestCase. Django's TestCases are not supported because they attempt to purge the database. Turn this flaw into a feature by segregating testcases into those that either need or don't need the test database. This tool may not be useful in all cases, but in certain cases you can have more rapid testing iterations. I use it for certain utility applications.
**Setup:**
Place in <app_name>/management/commands/unsafe_test.py
**Run:**
$./manage.py unsafe_test <app_name>
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.