Using Django Forms to display and edit?

前端 未结 4 507
天命终不由人
天命终不由人 2021-02-09 01:36

I\'m wrestling with how to best create HTML pages in Django that can either be used for displaying or editing data. That is, I\'d like the field\'s values to appear as text in d

4条回答
  •  长发绾君心
    2021-02-09 02:18

    Have I missed something

    Forms not only display the field widgets but also handle the post data. A post sent would cause it to process the data cleanup, form and field error handling, etc.

    It's kind of breaking the pattern - why create and render a form object only to tweak it not to look like a form?

    If you are worried about too much work on templates, try to solve it with template inheritance in a best possible way. If you are very sure you want to change only the field tag, you can make sth like

    {% if form %}
        {% for error in form.field.errors %}
            {{ error|escape }}
        {% endfor %}
        {{ form.field }}
    {% else %}
        {{ object.field }}
    {% endif %}
    

    for every field, but IMO that's not the point, YMMV.

    Also what comes to mind (thinking of your solution) is dynamically attach widgets to form fields, but that'd be overengineering.

提交回复
热议问题