Django Simple Captcha - Manually displaying the Captcha field in a template

1,151 views
Skip to first unread message

mmuk2

unread,
Mar 4, 2013, 9:10:42 PM3/4/13
to django...@googlegroups.com
Hi,

I'm currently trying to implement django simple captcha on my form. It's quite straight forward to set up with the "out of the box" django form functionality - just add a captcha field to the form in forms.py, and in the template, use the built in django form template like this:

{% form.as_ul %}

And this dynamically generates the HTML for all my form fields including the captcha field:

<label for="id_captcha_1">Captcha:</label>
<img class="captcha" alt="captcha" src="/captcha/image/7114a37ed0f72ec3105510e0f5c73128a2f95649/">
<input id="id_captcha_0" type="hidden" value="7114a37ed0f72ec3105510e0f5c73128a2f95649" name="captcha_0">
<input id="id_captcha_1" type="text" name="captcha_1">

My issue is that I can't use {% form.as_ul %} to display my form, because each field requires customised css styling.

So I'll need to replace the {% form.as_ul %} with the actual HTML code in my template.

This works fine, however, the captcha field value needs to be re-generated each time, so I need to generate the captcha field value dynamically each time the form is loaded.

What is the best way to do this? I guess I'm looking for something similar to the way the csrf token works, ie. the {% csrf_token %} tag, which dynamically generates the csrf token. Is there an equivalient for simple captcha, like {% captcha_value %} or something?

thanks in advance,
Mark

Alexander Tyapkov

unread,
Oct 3, 2013, 5:42:33 AM10/3/13
to django...@googlegroups.com
Hi Mark,
 Interesting if you finally have found some solution or not?

mmuk2

unread,
Oct 3, 2013, 6:39:16 PM10/3/13
to django...@googlegroups.com
Hi Alexander,

Although I no longer use captcha on the form in question anymore, I recall that I did eventually get it working by adding something like the following in my html to generate the captcha component:

<label for="{{ signup_form.captcha.auto_id }}">{{ signup_form.captcha.label }}</label>
        {{ signup_form.captcha }}<span class="error">{{ signup_form.captcha.errors }}</span>

thanks,
Mark
Reply all
Reply to author
Forward
0 new messages