Is there a simple way to make Django render {{myform.name}}
as
John Smith
instead of
<input id="id_name" name="name" value="John Smith" />
inside <form>
tags? Or am I going about this the wrong way?
Is there a simple way to make Django render {{myform.name}}
as
John Smith
instead of
<input id="id_name" name="name" value="John Smith" />
inside <form>
tags? Or am I going about this the wrong way?
<form>
{% for field in form %}
{{ field.label }}: {{ field.initial }}
{% endfor %}
</form>
Take a look here Form fields and Working with forms
You can also use a new widget: I did this so that I could have a widget that created a text display of a date, and a hidden form with the same date in it, so it could be visible to the user, but they cannot change it.
Here is an initial (still testing/to be cleaned up) version:
class DayLabelWidget(forms.Widget):
def render(self, name, value, attrs):
final_attrs = self.build_attrs(attrs, name=name)
if hasattr(self, 'initial'):
value = self.initial
if type(value) == type(u''):
value = datetime.date(*map(int, value.split('-')))
return mark_safe(
"%s" % value.strftime("%A (%d %b %Y)")
) + mark_safe(
"<input type='hidden' name='%s' value='%s' />" % (
name, value
)
)
def _has_changed(self, initial, data):
return False
You then use this in the field as (widget=DayLabelWidget,)
.
I think this is what you want: http://code.djangoproject.com/ticket/10427
I patched my django and voila...