Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change form layouts in Django 1.8

I have a form

Field Name: [Input Box]

I want

Field Name:
[Input Box]

How can I achieve this?

forms.py

class SearchForm(forms.Form):
    search = forms.CharField()

views.py

form = SearchForm()
html_dtc = {'form':form}
return render_to_response('site/home.html', html_dtc)

home.html

<form method='POST' action=''> {% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="btn btn-success btn-sm">Update</button>
</form>

Thank you :)

like image 358
derrend Avatar asked Aug 13 '15 09:08

derrend


4 Answers

generally I don't recommend use the HTML code generated by Django, but instead I supply what is needed by the DJANGO form.

but some are required: like the ERRORs, like the CSRF token.

let me add some examples to clarify what I am talking

<form class="*" style="*">
    <label /><input name="email" />
    <label /><input name="password" />
<form>

basically what I am suggesting is, do not use template tags unless absolute necessary like CSRF.

in this way, you can completely separate the design from the backend logic. you can have front end work indecently on the UI. the interface is the form fields, you have to supply all fields to the backend. like in this case 'email' && 'password' is required at backend

like image 155
zinking Avatar answered Oct 03 '22 00:10

zinking


You want a custom form rendering. You can read more about it here. For example, the following code would achieve what you're after.

<form method='POST' action=''> {% csrf_token %}
    {% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field.label_tag }} <br/>
        {{ field }}
    </div>
    {% endfor %}
    <button type="submit" class="btn btn-success btn-sm">Update</button>
</form>

(field.errors are added, because when you are manually rendering the form, you have to take care of error rendering as well)

like image 9
zanderle Avatar answered Nov 16 '22 19:11

zanderle


Try to overide form.as_p()

class SearchForm(forms.Form):
   search = forms.CharField()

   def as_p(self):
    "Returns this form rendered as HTML <p>s."
     return self._html_output(
        normal_row='<p%(html_class_attr)s>%(label)s <br> %(field)s%(help_text)s</p>',
        error_row='%s',
        row_ender='</p>',
        help_text_html=' <span class="helptext">%s</span>',
        errors_on_separate_row=True)
like image 3
Geo Jacob Avatar answered Nov 16 '22 20:11

Geo Jacob


If this is a one off thing you can render your form manually like described here in the documentation.

Otherwise there's the django-floppyforms which gives you great control over how forms and (default) widgets are rendered. Just define a custom layout, make it the default, use floppyforms custom Form classes (they behave exactly the same) and you're good to go.

As far as I remember some of floppyforms's functionality will also be included in Django 1.9, so look out for that, too.

like image 2
Chris Schäpers Avatar answered Nov 16 '22 18:11

Chris Schäpers