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)
Based on the original http://code.google.com/p/django-thumbs/
Added South support too.
Usage:
` photo = ImageWithThumbsField(upload_to='images', sizes=((125,125,True),(300,200),)`
To retrieve image URL, exactly the same way as with ImageField:
`my_object.photo.url`
To retrieve thumbnails URL's just add the size to it:
`my_object.photo.url_125x125`
`my_object.photo.url_300x200`
To convert to greyscale set the third attribute of size to True
There is a lot of debate on whether there is a real future for the Django CBVs (class based views). Personally, I find them tedious, and just wanted a way to keep my views clean.
So, here is a really minimalistic way of having class based views, without the fuss.
This is a fork from:
http://stackoverflow.com/questions/742/class-views-in-django
http://djangosnippets.org/snippets/2041/
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!'}
This is an Authorization class for [Tastypie](http://django-tastypie.readthedocs.org/en/latest/authentication_authorization.html) v0.9.11 (v0.9.12 changes how Authorization works).
DjangoAuthorization checks specific permissions — `add_model`, `change_model`, `delete_model`, etc. If you don't need that level of permissions checking, this might be useful. It just makes sure the User is logged in. It's equivalent to the `login_required` decorator.
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>
This is a simpel snippet to prevent conflict between Django and AngularJS template syntax.
It is possible to change the AngularJS syntax, but this can cause compatibility problems, so I figured that this was a better solution.
Difference from standard Django 1.4 implementation is just structure of list. In django `<input>` elements are *wrapped* by their labels, and look like this::
<ul>
<li><label><input/> Label 1</label>
<li><label><input/> Label 2</label>
</ul>
This widget puts labels *after* input elements::
<ul class="form-button-radio">
<li><input/> <label>Label 1</label>
<li><input/> <label>Label 2</label>
</ul>
It makes possible to define style for both inputs and labels, hiding inputs and making labels look like pressable buttons. No javascript needed, just CSS (see docstring).
To auto-submit the form when pressing a button, JQuery can be added::
<script>
$(document).ready(function() {
$('ul.form-button-radio input[type="radio"]').change(function() {
$(this).parents('form').submit();
});
});
</script>
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.