Login

Snippets by jeverling

Snippet List

Simple random file CAPTCHA

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/)

  • captcha
  • random
Read More

Rendering radio-buttons with icons instead of labels

I was looking for a way to save screen real estate, by using icons instead of labels for my list of choices, which in addition should be displayed as horizontal radio buttons. For example, I wanted to use thumbs_up.gif instead of "approve". I found a HorizontalRadioRenderer here: [https://wikis.utexas.edu/display/~bm6432/Django-Modifying+RadioSelect+Widget+to+have+horizontal+buttons](https://wikis.utexas.edu/display/~bm6432/Django-Modifying+RadioSelect+Widget+to+have+horizontal+buttons) Thanks to Barry McClendon for this snippet! At first, I tried to achieve display of icons instead of labels by modifying the render method, but after a while I gave up on that and decided to just use the choices tuple. This doesn't work too well with a select box (no icons, no text), but in combination with a radio widget it looks quite nice. If you mark the strings for translation, you can also easily change icons, alt and title for each language.

  • forms
  • choices
  • choicefield
  • render
  • radiobuttons
  • icons
Read More

P3P Headers for iframes

This is nothing fancy and hasn't much to do with django itself, I just searched for this information for quite a while and thought it may be useful for others. If you use IE7 (and maybe IE6), it will block cookies in iframes, if the iframes content comes from another server (quite common, I think). The P3P specification lets you declare your privacy settings in a format interpretable by browsers, essentially you can tell IE that you adhere to "don't be evil", and are allowed to handle cookies afterwards. I don't think that makes much sense, but it seems that it is the only way to make IE accept cookies in iframes. I had no idea that django made it that incredibly easy to "patch" the response-header, but it does! :)

  • p3p
  • ie7
  • internet
  • explorer
  • iframe
  • privacy
Read More

jeverling has posted 3 snippets.