Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom template: key for retrieving hashkey value #237

Open
venzen opened this issue Dec 2, 2024 · 4 comments
Open

Custom template: key for retrieving hashkey value #237

venzen opened this issue Dec 2, 2024 · 4 comments

Comments

@venzen
Copy link

venzen commented Dec 2, 2024

I've created a custom template as described in the docs at Rendering.

As per the code example I am able to populate the image_url with {{ image }}:

<img src="{{ image }}" alt="captcha" class="captcha" />

Which key do I use to populate the hidden input value (hashkey) of "id_captcha_0" in the following?

<input type="hidden" name="captcha_0" value="{{ ??? }}" required id="id_captcha_0" autocomplete="off">
@mbi
Copy link
Owner

mbi commented Dec 2, 2024

See Context, one paragraph down in the linked documentation. Is this what you're looking for?

@venzen
Copy link
Author

venzen commented Dec 2, 2024

Perhaps I'm not understanding the Context environment so well, but I tried using {{ key }} and {{ hidden_field }}. Both failed to populate the value.

Creating a new key from my view function and passing that in the context, was not accessible to the template.

As I say I might be misunderstanding something. Just to explain: my app function "contact_view()" is rendering an app template called "contact_page.html" which includes the captcha form "custom_captcha_template.html". Somehow, context is visible to the "contact_page.html" but is not being rendered by the custom template.

@mbi
Copy link
Owner

mbi commented Dec 2, 2024

The custom_captcha_template.html template included from your parent template doesn't get the context from the form rendering, only the form rendering itself does!

You want to define a form, which includes a CaptchaField, as described in the docs

class CustomCaptchaTextInput(CaptchaTextInput):
    template_name = 'custom_field.html'

class CaptchaForm(forms.Form):
    captcha = CaptchaField(widget=CustomCaptchaTextInput) 

Then from either of your templates you render the form, e.g. {{form}} or {{form.as_p}} and the template defined in the customized CaptchaTextInput widget will receive the context variables while being rendered.

@venzen
Copy link
Author

venzen commented Dec 2, 2024

Thank you. Your explanation is logical and that is how I understand after reading the docs. I'll check my implementation again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants