Skip to content

Instantly share code, notes, and snippets.

@NiMeDia
Created September 17, 2020 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiMeDia/c368a98c08385191d3424180afed0aaf to your computer and use it in GitHub Desktop.
Save NiMeDia/c368a98c08385191d3424180afed0aaf to your computer and use it in GitHub Desktop.
Django textarea with default value (quick&dirty)
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'})
<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