javascriptdjangodjango-widget-tweaks

Pass a string to javascript function from render_field tag in Django


I can pass a string value to javascript function onblur when using plain html tag as shown below:

<input type="password"  name="l_password" onblur="passwordValidation(this,'id_lpassword_error')" />

but when i try to do the same thing for render_field tags it doesnt work. i get error TemplateSyntaxError: Could not parse the remainder

{%render_field form.password onblur="passwordValidation(this,'id_lpassword_error')" %}

how can i pass the string 'id_lpassword_error' to a javascript function from the render_field tag in Django?


Solution

  • render_field is doing its own custom parsing of that tag, and it looks like it treats double quotes and single quotes the same. So it is probably looking for the tag to finish after your first single quote.

    It looks like using the filter attr should work, since it is using Django's built-in template tag parsing system, which almost definitely can deal with different types of quotes properly.

    So, if I'm understanding this right, something like:

    {{form.password|attr:"onblur:passwordValidation(this,'id_lpassword_error')" }}

    Let us know if that works.