Created
September 17, 2020 09:46
-
-
Save NiMeDia/c368a98c08385191d3424180afed0aaf to your computer and use it in GitHub Desktop.
Django textarea with default value (quick&dirty)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
class TextareaWithValueHandling(forms.Textarea): | |
""" | |
Ugly hack to get a default value preset within a textarea. (not supported by django) | |
This template renders widget.attrs.value within the textarea body if there is no real value | |
""" | |
template_name = 'form/widget/textarea_with_value_handling.html' | |
class TheForm(forms.Form): | |
the_field = TextareaWithValueHandling(attrs={'value': 'the freakin default value'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}> | |
{% if widget.value %}{{ widget.value }}{% elif widget.attrs.value %}{{ widget.attrs.value }}{% endif %}</textarea> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment