htmldjango-templates

How to resolve the following HTML error?


I have the following lines in my django template file:-cart.html:-

<td class="right">
    <form method="post" action="." class="cart">
        <label for="quantity">Quantity:</label>
        <input type="text" name="quantity" value="{{ item.quantity }}" id="quantity" size="2" class="quantity" maxlength="5" />
        <input type="hidden" name="item_id" value="{{ item.id }}" />
</td>
<td>
    <input type="submit" name="submit" value="Update" />
    </form>
</td>

At the first closing tag of td i.e is the first </td>,i get the following error: Element form is not closed.Please help me to rectify this error.


Solution

  • HTML tags must be structured in a directly hierarchic manner. Closing a tag that was opened inside previously closed elements is incorrect and will often produce errors and issues.

    Enclose the form inside the table data, like this:

    <td class="right">
        <form method="post" action="." class="cart">
            <label for="quantity">Quantity:</label>
            <input type="text" name="quantity" value="{{ item.quantity }}" id="quantity" size="2" class="quantity" maxlength="5" />
            <input type="hidden" name="item_id" value="{{ item.id }}" />
            <input type="submit" name="submit" value="Update" />
        </form>
    </td>
    

    or enclose either the whole table or the table elements inside the form, like this:

    <form method="post" action="." class="cart">
        <td class="right">
            <label for="quantity">Quantity:</label>
            <input type="text" name="quantity" value="{{ item.quantity }}" id="quantity" size="2" class="quantity" maxlength="5" />
            <input type="hidden" name="item_id" value="{{ item.id }}" />
        </td>
        <td>
            <input type="submit" name="submit" value="Update" />
        </td>
    </form>    
    

    I assume the latter solution is the one you're looking for.