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?
django-forms Using Model Form Making fields not editable Django 1.9 added the Field. disabled attribute: The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won't be editable by users.
CharField() is a Django form field that takes in a text input. It has the default widget TextInput, the equivalent of rendering the HTML code <input type="text" ...> . This field works well for collecting one line inputs such as name or address.
form. cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).
Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.
<form>
{% for field in form %}
{{ field.label }}: {{ field.value }}
{% endfor %}
</form>
Take a look here Form fields and Working with forms
Old topic, but I think some people still comes here.
You can do something like this too:
from django.utils.safestring import mark_safe
class PlainTextWidget(forms.Widget):
def render(self, _name, value, _attrs):
return mark_safe(value) if value is not None else '-'
And in your form
class SomeForm(Form):
somename = forms.CharField(widget=PlainTextWidget)
Under Django 2.1+ you'll need the following:
from django.utils.safestring import mark_safe
class PlainTextWidget(forms.Widget):
def render(self, name, value, attrs=None, renderer=None):
return mark_safe(value) if value is not None else '-'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With