Several DEVICE forms can be displayed on a page (Order), as tabs. When a user closes a tab and hits save
, then renters that particular Order (ID), the user still sees his deleted tab. As a quick workaround, a checkbox was built into the html code but this is not desired.
Currently the code in my view.py
looks like this.
def edit_order(request,id = None, order_id = None):
order = Order.objects.get(id=id)
'''some stuff'''
if request.method == 'POST':
formCustomer = CustomerModelForm(request.POST,instance=Customer.objects.get(order=order.id))
formInfo = InfoModelForm(request.POST,instance=Info.objects.get(order=order.id))
DBFormSet = modelformset_factory(DB, extra=1, can_delete=True, form=LUNModelForm)
formset = DBFormSet(request.POST or None, queryset=DB)
if formset.is_valid():
for i,frm in enumerate(formset.forms):
if frm.cleaned_data['id']:
if frm.is_valid():
if not frm.cleaned_data['capacityGB'] > 0:
frm.cleaned_data['id'].delete()
elif frm.cleaned_data['DELETE'] == True:
frm.cleaned_data['id'].delete()
#frm.save() or formset.save() ??
else:
dev = frm.cleaned_data['id']
dev.capacity = frm.cleaned_data['capacity']
dev.save()
I suspected the matter that the tab was not remaining closed was an issue with "can_delete
", but it doesn't seem to make a difference when I added this. Then I read that the issue could be related to the formset.save
, which also didn't resolve anything. Below the html code:
html:
<ul class="nav nav-tabs" id="tab4Headers">
{% for form in formsetDEVICE.forms %}
{% if forloop.first %}
{% if form.errors %}
<li class="active"><a href="#tab4_{{forloop.counter}}" data-toggle="tab">DEVICE <i class="icon-warning-sign"></i> </a></li> <!-- section 4.1 -->
{% else %}
<li class="active"><a href="#tab4_{{forloop.counter}}" data-toggle="tab">DEVICE</a></li> <!-- section 4.1 -->
{% endif %}
{% else %}
{% if form.errors %}
<li><a href="#tab4_{{forloop.counter}}" data-toggle="tab"><button class="close" type="button" id="close-tab4_{{forloop.counter}}">×</button>DEVICE <i class="icon-warning-sign"></i> </a></i></li> <!-- section 4.2 ... 4.n -->
<!--{% if formsetDEVICE.can_delete %}
<li>{{ form.DELETE }}</li>
{% endif %}-->
{% else %}
<li><a href="#tab4_{{forloop.counter}}" data-toggle="tab"><button class="close" type="button" id="close-tab4_{{forloop.counter}}">×</button>DEVICE</a></li> <!-- section 4.2 ... 4.n -->
<!--{% if formsetDEVICE.can_delete %}
<li>{{ form.DELETE }}</li>
{% endif %}-->
{% endif %}
{% endif %}
{% endfor %}
</ul>
Since it is not clear how to delete the tab via closing the tab (and then hit the save
button), the current work around is simply to insert a section on the tab with a checkbox included, as so:
{# --- delete item --- #}
{% if form.instance.pk %}
{# render row for DELETE-flag only for forms with existing instance #}
<tr>
<td colspan="2">
</td>
<td colspan="2">
edit Action required: <br/><br/>
{{form.DELETE|safe}} delete this DEVICE from the Order.
</td>
</tr>
{% endif %}
{# --- END delete item --- #}
The current workaround works, but this is not the desired functionality. I've tried some of the suggestions as seen here, but to no avail. I think the problem is more related to the link between the html and the view.py (so something to do with frm.cleaned_data['DELETE']
though I could be wrong.
EDIT:
I suspect to get around this, js code and/or css needs to be inserted.