This is a snippet for a simple CAPTCHA. A random image from a list of images is shown, and the form checks if the correct solution was given.
Normally I would use django-simple-captcha or maybe reCAPTCHA, but in this case I wanted to have a number of fixed images, nothing dynamically generated.
I wanted to include the contact form in multiple pages, most of which are `direct_to_template` generic views.
However, passing the random image to the `extra_context` of `direct_to_template` didn't work, because the value was only initialized once on startup.
Therefore I pass the list of possible choices to `extra_context`, and use the template filter `|random` to select one image. The form's clean method will check if the correct solution was given when `form.is_valid()` is called in the view. If not, the view will display a new captcha.
Of course there are other, more elegant solutions like a custom template tag or overriding the generic view, but this works fine for me. Using a fixed number of images will probably not provide good protection for sites that are of much interest to spammers, but for smaller sites it should be sufficient.
You can see the CAPTCHA in action at [http://www.lackieren-in-polen.de/](http://www.lackieren-in-polen.de/)
CitySelector is a jquery widget, allowing to pick cities from DB, filled by django_ipgeobase application.
Also it includes widget for django forms, based on mentioned jquery plugin, views and urlconf, required to provide
interaction between widget and DB and middleware, populating request with correspondent location.
Visit https://bitbucket.org/JustDelight/city_selector
Similar to `{% spaceless %}`, but only removes spaces followed by a special marker (`&nosp;`).
Helps keeping proper indentation:
{% skipspaces %}
Lots of coffee
{% if from_brazil %}
from Brazil
{% else %}
from Columbia
{% endif %}
{% if tea %}&nosp;, some tea{% endif %}
and a cake.
{% endskipspaces %}
Otherwise you'd have to write it in one looong line or get used to living with spaces before commas :)
NB the marker itself is stripped from rendered html as well.
You only have to put the code in some of your models.py and to have django-fiber installed in your system. Every image uploaded using Fiber admin dialog will be automatically resized and thumbnailed. Or whatever you need to do with it.
This snippet is inspired by [dnordberg](http://djangosnippets.org/snippets/1048/) 's translation script which used google's translation script. But after google translation is no more free. I put together it to use microsoft translator's python wrapper by [openlab](https://github.com/openlabs/Microsoft-Translator-Python-API)
usage
-----
1. get a bing appID from [here](https://ssl.bing.com/webmaster/developers/appids.aspx) and replace it on the top of script
2. sudo apt-get install translate-toolkit
3. sudo pip install microsofttranslator
4. sudo autotranslate.py [pofile] [sourcelangcode] [targetlangcode]
Enjoy!!!
**Prabhat Kumar Gupta**
Python/Django Dev
[http://www.prabhatgupta.com](http://www.prabhatgupta.com)
You'll need to `pip install pyScss` first.
Converts on the fly, so you won't want to use this for much more than just testing.
Usage in a template:
{% load sass %}
{% include_sass "disclosures/css/base.scss" %}
{% include_sass "disclosures/css/grid.scss" %}
Until [#11154] is fixed, django won't create permissions for proxy models for you as it does with other models, even the one you define using the [`permissions` option](https://docs.djangoproject.com/en/dev/ref/models/options/#django.db.models.Options.permissions) on your models.
This snippet will make sure all permissions are create for proxy models. Just make sure this code gets loaded after the `update_contenttypes` handler is registered for the `post_syncdb` signal. Putting this code in one of your `INSTALLED_APPS' after `django.contrib.contenttypes' should do it.
The Django admin site has a feature to filter objects in change list by parameters supplied in the query string. Particularly, parameters such as date\__gte and date\__lte can be used.
This example is for filtering objects in change list by a date range (the date field is called expiration_date, but you can, of course, use any other name).
As the server side (Django) already supports such filtering, all you need to do is to edit this for your needs (you can also add some DRY if you want) and put it into templates/admin/app_name/model_name/change_list.html.
Some CSS for better layout:
div.date_filter select#from_month, div.date_filter select#to_month {
margin-left: 0;
}
div.date_filter label {
margin-right: 5px;
}
This is a simplest approach possible. `as_view()` is replaced, so
that it applies the given decorator before returning.
In this approach, decorators are always put on top - that means it's not
possible to have functions called in this order:
B.dispatch, login_required, A.dispatch
NOTE: By default this modifies the given class, so be careful when doing this:
TemplateView = view_decorator(login_required)(TemplateView)
Because it will modify the TemplateView class. Instead create a fresh
class first and apply the decorator there. A shortcut for this is
specifying the ``subclass`` argument. But this is also dangerous. Consider:
@view_decorator(login_required, subclass=True)
class MyView(View):
def get_context_data(self):
data = super(MyView, self).get_context_data()
data["foo"] = "bar"
return data
This looks like a normal Python code, but there is a hidden infinite
recursion, because of how `super()` works in Python 2.x; By the time
`get_context_data()` is invoked, MyView refers to a subclass created in
the decorator. super() looks at the next class in the MRO of MyView,
which is the original MyView class we created, so it contains the
`get_context_data()` method. Which is exactly the method that was just
called. BOOM!
Upload an image with file-uploader from http://github.com/valums/file-uploader
and save it to disk with the snippet and also to database if you want to
Out of the box, Django e-mail fields for both database models and forms only accept plain e-mail addresses. For example, `[email protected]` is accepted.
On the other hand, full e-mail addresses which include a human-readable name, for example the following address fails validation in Django:
Joe Hacker <[email protected]>
This package adds support for validating full e-mail addresses.
**Database model example**
from django import models
from full_email.models import FullEmailField
class MyModel(models.Model):
email = FullEmailField()
**Forms example**
from django import forms
from full_email.formfields import FullEmailField
class MyForm(forms.Form):
email = FullEmailField(label='E-mail address')
I maintain this code in a [GitHub gist](https://gist.github.com/1505228). It includes some unit tests as well.
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.