Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django forms adding <div> after form field

Below is my form code :

class FMessage(forms.Form):
    From = forms.CharField()
    To = forms.CharField()
    Subject = forms.CharField()
    Message = forms.CharField()

and this is my html code:

<form method='POST' action='.'>
    {% csrf_token %}
    {{ form.as_p }}
    <input type='submit' value='submit'>
</form>

The code works fine by displaying forms and has not any issue in functionality, but now I need to wrap my form fields in html by a div like this:

<div id='mydiv'>
    <input ... />
<div>

How can I fix it?

like image 876
Friend Avatar asked Mar 26 '13 06:03

Friend


People also ask

What is Is_valid () do in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute. Let's see an example that takes user input and validate input as well.

How do I override a form in Django?

You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form.

What is the difference between form and ModelForm in Django?

The similarities are that they both generate sets of form inputs using widgets, and both validate data sent by the browser. The differences are that ModelForm gets its field definition from a specified model class, and also has methods that deal with saving of the underlying model to the database. Save this answer.

How do I add a class or id attribute to a Django form field?

In order to add a class or id attribute to a form in Django, we have to place a widget=form. TextInput (or widget= form. EmailInput, if email input) within the form field (because it's a text field). Inside of this widget, we then have to put, attrs={'class':'some_class'}.


2 Answers

Seems like you do not really want to use the inbuilt <p> or <table> wrapped forms and rather want to display the fields wrapped within a <div>'s. You can simply iterate over fields in the form as follows.

{% if form %}
    <!-- Form Errors -->
    {% if form.errors %}
        <ul class="errors">
            {% for error in form.errors %}
                <li>{{ error }}</li>
            {% endfor %}
        </ul>
    {% endif %}

    <!-- Display Form -->
    <form>
    {% csrf_token %}
    {% for field in form %}
        <div class="mydiv">
            <label class="mylabel">{{ field.label }}</label>
            {{ field }}
        </div>
    {% endfor %}
    </form>
{% endif %}
like image 85
Amyth Avatar answered Oct 16 '22 22:10

Amyth


Dont render the form by using form.as_p. You need to show each field of the form, for example, by using form.to. By using this way, you can wrap the field 'to' into a div

 <div>{{ form.To}} </div>

For more detail, view this link

like image 7
Thai Tran Avatar answered Oct 16 '22 20:10

Thai Tran