txt2img tag shows on the web text as images, helping to avoid get indexed email address and some other information you don't want to be on search engines.
Usage:
`{{worker.email|txt2img:18|safe}}`
An "if-style" template tag that checks to see if a user belongs to a one or mores groups (by name).
Usage:
`{% ifusergroup Admins %} ... {% endifusergroup %}
or
{% ifusergroup Admins Clients Programmers Managers %} ... {% else %} ... {% endifusergroup %}`
This is a decorator which will gets Django to try the cache before computing the result of a function. It automatically builds the cache key as a hash of the function name and inputs, and allows you to set whatever timeout you want.
The snippet is a modification of [snippet 1315](http://djangosnippets.org/snippets/1315/) to fit the needs for Django 1.3 and 1.4. You can follow the explanations and instructions there.
To plot a nice and so useful call-graph with timings, call:
$ gprof2dot -f pstats unittest.profile | dot -Tpng -o unittest.profile.graph.png
where 'unittest.profile' is the test runners profile output defined in your settings.
This is just a modified version of a [previous snippet](http://djangosnippets.org/snippets/1364/) to make it work with unicode and with class-based ListView paginator_class
To use it put this in your urls.py:
`from youapp.fileyouchose import NamePaginator`
`urlpatterns = patterns('',`
`url(r'^example/(?P<page>[0-9]+)/$', ListView.as_view(model=myModel,template_name="mytemplate.html",paginator_class=NamePaginator,paginate_by=25), name="url_name"),`
And then in your template something like this would work:
{% if page_obj.has_other_pages %}
<div class="row">
<div class="span12">
<div class="pagination">
<ul>
{% if page_obj.has_previous %}
<li><a href="{% url page page=page_obj.previous_page_number %}">Prev</a></li>
{% else %}
<li class="disabled"><a>Prev</a></li>
{% endif %}
{% for p in page_obj.paginator.pages %}
<li {% if p == page_obj %}class="active"{% endif %}>
<a href="{% url category_page page=p.number %}">{{ p }}</a>
</li>
{% endfor %}
{% if page_obj.has_next %}
<li><a href="{% url page page=page_obj.next_page_number %}">Next</a></li>
{% else %}
<li class="disabled"><a>Next</a></li>
{% endif %}
</ul>
</div>
</div>
</div>
{% endif %}
When uploading a file or image, you need to put it somewhere that's not going to be orphaned by a change in the model. You could use a globally unique value like a uuid, but the django id autofield is a much shorter surrogate field that can be used in a friendly path to the object. The problem with id autofields is that they don't exist when the object is created for the first time - so all files using the id get uploaded to a location 'None' on first save, increasing the likelihood of name collisions.
This postgresql only snippet fetches the next id in a way that is safe for concurrent access.
This snippet only works on postgresql because neither sqlite or mysql have a nextval equivalent. Instead you would have to lock the table for writes and select the last value inserted incrementing it yourself.
This BasePasswordHasher allows the easy migration of passwords from Drupal to Django 1.4. Drupal stores its passwords using a SHA512 hash, but with some iterations and postprocessing.
This snippet allows you to migrate the username and passwords over seamlessly- the only necessary change is truncating the first character of each password hash (since Django 1.4 stores each password as algorithm$hash).
Note that this snippet *requires* Django 1.4, but there is no option for that snippet in the list.
Provided as a github gist [here](https://gist.github.com/2344345).
This allows the mod_xsendfile module for Apache safely serving private files. Django take cake about processing and permissions checking, Apache server requested files.
Installation of mod_xsendfile:
$ tar -xzvf mod_xsendfile-0.12.tar.gz
$ /usr/sbin/apxs -c mod_xsendfile-0.12/mod_xsendfile.c
$ ld -Bshareable -o mod_xsendfile-0.12/mod_xsendfile.so mod_xsendfile-0.12/mod_xsendfile.o
Copy mod_xsendfile.so to your local Apache modules folder.
Modify httpd.conf to load an enable the module:
LoadModule xsendfile_module modules/mod_xsendfile.so
Add to virtual host container:
<Virtual ...:80>
XSendFile On
XSendFilePath /home/django_projects/mysite/media/
</Virtual>
In case you ever use [requests](http://python-requests.org/) (or [slumber](http://slumber.in/)) to do requests against a Tastypie API that requires API key authentication, this small custom auth class will help you.
Use it like that (with requests):
auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252')
response = requests.get('http://api.foo.bar/v1/spam/', auth=auth)
or with slumber:
auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252')
api = slumber.API("http://api.foo.bar/v1/", auth=auth)
spam = api.spam().get()
This snippet provides getting templates from the model in database.
We work with templates as usual (using as a template name value of the field **"slug"**).
You can do your own application without "TemplateTypes" model - it's added for ability to filter templates. You can use choices or remove "template_type" field and "TemplateTypes" model at all.
For ease of editing, you can connect all this to the admin interface, adding to the field "template_body" widget with syntax highlighting (I used [CodeMirror](http://codemirror.net/)).
The problem with supplying a Django model field with choices parameter is the way you check a value of that field in an object. You do nasty things like this:
if model_instance.choice_field == 1:
The problem of getting rid of hard-coded numbers is recognized over the internet, but I haven't found any short and understandable solution. Basically, we need a enumeration in python, that is ok to use as the Django `choices` model field argument.
I've seen a couple of solutions of DjangoSnippets. Mine is shorter and easier because it only works for integer field choices.
As of django 1.4, the newly introduced `bulk_create()` function has a strong limitation when using SQLite. Namely it causes an error if you want to create more than `999/F` objects, where `F` is the number of fields in the object type.
The above `safe_bulk_create(objs)` function solves this issue by splitting the list of objects to appropriately sized bulks.
This solution also works fine with other db backends, and according to my experiments, it causes no significant overhead comparing to using `bulk_create()` directly.
For more details on the issue, see
https://code.djangoproject.com/ticket/17788
Thanks to charettes for pointing out how to calculate the number of fields in an object.
This is the description of a custom template tag to create DRY menu. It solves the problem of markup duplication in templates of your site. The menu always has one active option and one or several inactive options.
HOW TO USE
Define a structure of your menu in a parent template:
{% defmenu "menu1" %}
{% active %}<span class='active'>__text__</span>{% endactive %}
{% inactive %}<a href='__url__'>__text__</a>{% endinactive %}
{% opt "opt1" "/opt1/" %}Go to opt1{% endopt %}
{% opt "opt2" "/opt2/" %}Go to opt2{% endopt %}
{% opt "opt3" "/opt3/" %}Go to opt3{% endopt %}
{% enddefmenu %}
The menu has it's name (first parameter of the tag 'defmenu'.
First parameter of a tag 'opt' is menu option's name. '__text__' inside of 'active'/'inactive' will be substituted by inner text of a tag 'opt' (Go to opt...), '__url__' indide of 'active'/'inactive' will be substituted by second parameter of a tag 'opt'
To generate menu with one selected option in child template do:
{% menu "menu1" "opt1" %}
Here: "menu1" is a name of menu that was defined by 'defmenu' tag, "opt1" is selected option.
Result of the applying 'menu' is the next:
<span class='active'> Go to opt1</span> <a href='"/opt2/"'>Go to opt2</a> <a href='"/opt3/"'>Go to opt3</a>