Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a value as text instead of field inside a Django Form

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?

like image 505
ine Avatar asked Jul 15 '09 21:07

ine


People also ask

How do you make a field non editable in django?

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.

Is a django form field that takes in a text input?

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.

What is form Cleaned_data in django?

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).

How can I get form data in django?

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.


2 Answers

<form>
    {% for field in form %}
            {{ field.label }}: {{ field.value }}
    {% endfor %}
</form>

Take a look here Form fields and Working with forms

like image 189
tefozi Avatar answered Sep 20 '22 17:09

tefozi


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 '-'
like image 30
caio Avatar answered Sep 20 '22 17:09

caio