开发者

Instantiate a modelform from a given instance in a template

I need to make quick changes to some instances in a object_list view, I've chosen to do this with some level of progresive enhacement using forms for each object in the object_list which ends up looking something like this:

<ul>
{% for column in columns %}
<li class="{% if not column.is_available %}disabled{% endif %}">
    <h3>{{column.pk}}. <a href="/column/{{column.pk}}">{{column.name}}</a></h3>
    <a href="/column/{{column.pk}}">Registros con esta columna</a>
    <form action="/column/{{column.pk}}" method="post" class="column_form">
    {% csrf_token %}
    <input type="hidden" name="object_id"  value="{{column.pk}}"/>
    <input type="hidden" name="name"  value="{{column.name}}"/>
    <input type="hidden" name="label"  value="{{column.label}}"/>
    ...
    {{ column_form.data_type }}
    <label>geográfico</label><input type="checkbox" name="has_geodata" {% if column.has_geodata %}checked{% endif %}/>
    <label>disponible</label><input type="checkbox" name="is_available"  {% if column.is_available %}checked{% endif %}/>
    {% if columns.has_geodata %}
    {{columns_form.geodata_type}}    
    {% endif %}
    </form>
</li>
{% endfor %}
</ul>

The catch is that I don't have a simple way to pass the object's instance to the form class in the template's for loop so it has the proper values for each field.

Is there a proper way to handle this scenario?

Should I make this some kind of inlin开发者_如何学编程e form?


You could create a template tag that takes in your object instance and then either adds the form to the context, or renders the form in place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜