Django: For Loop to Iterate Form Fields

前端 未结 4 1001
半阙折子戏
半阙折子戏 2021-02-07 10:09

I don\'t want to use django\'s built in form generation, seeking to specify each field in my template in order to customize the html output.

How do I iterate over a ser

4条回答
  •  一向
    一向 (楼主)
    2021-02-07 11:09

    For any frontend developers looking to customize a Django form you can use a package called django-widget-tweaks to render fields individually. Example code below:

    {# Your template with the form #}
    {% extends "base.html" %}
    {% load widget_tweaks %}
    
    
    {% csrf_token %} {% for field in form %} {% render_field field %} {% endfor %}

    Note: You'll want to make this look nicer of course, and you may want to loop through your form errors if there are any.

    They have some very useful examples on their PyPi page as well.

提交回复
热议问题