Issue
I am trying to generate a unique ID for each iteration of a for loop item. The item generated is a drop down menu and the number of times it s generated will depend on the number of incidents that are active. The the problem I am having is that the ID of the dropdown is the same for each dropdown menu. In this case id="id_status" when viewed via the browser page source
Is there a way to generate for example id="id_status_1" for the first dropdown, then id="id_status_2" for the second one and so on?
I tried to implement something with forloop.counter and also javascript's setAttribute("id", "uniqueIdentifier"), but have been racking my brain for hours now without success
models.py
from django.db import models
...
...
class IncidentStatus(models.Model):
status = models.CharField(max_length=128)
def __str__(self):
return self.status
forms.py:
from .models import IncidentStatus
...
...
class IncidentStatusAssigneeForm(ModelForm):
status = forms.ModelChoiceField(queryset=IncidentStatus.objects.all().order_by('status'), empty_label='Select a Status', label='')
class Meta:
model = IncidentStatus
fields = ('status',)
views.py: (for formset attempt)
from .models import IncidentStatus
from .forms import IncidentStatusAssigneeForm
from django.forms import modelformset_factory
...
...
def set_status(req):
all_incidents = Incident.objects.all
test_status_formset = modelformset_factory(IncidentStatus, form=IncidentStatusAssigneeForm)
test_status = test_status_formset()
...
...
context = {'test_status': test_status, 'all_incidents': all_incidents, ....}
return render(req, 'incident_main/assignee.html', context)
original views.py:
from .models import IncidentStatus
from .forms import IncidentStatusAssigneeForm
...
...
def set_status(req):
test_status = IncidentStatusAssigneeForm()
all_incidents = Incident.objects.all
...
...
context = {'test_status ': test_status, 'all_incidents': all_incidents, ....}
return render(req, 'incident_main/assignee.html', context)
template.html:
{% for incident in all_incidents %}
...
...
{{ test_status }}
...
...
{% endfor %}
view page source result
<select name="status" required id="id_status">
<option value="" selected>Select a Status</option>
<option value="9">Acknowledged</option>
<option value="12">General status update</option>
<option value="2">In Progress (assessment and diagnostics)</option>
<option value="10">In Progress (repairs/remedial action)</option>
<option value="3">Pending (awaiting feedback from requestor)</option>
<option value="5">Pending (awaiting parts/tools)</option>
<option value="4">Pending (call logged with service provider)</option>
<option value="7">Resolved</option>
</select>
The above result will be the same for all iterations of the for loop, as each dropdown is tied to its own task. As can be seen the ID generated is id="id_status". This is the value I am trying to make unique for each dropdown
EDIT: Added attempt code for formset. Only change is to views.py and the html template
Solution
I'll put this as an answer for now, since it's too long for a comment, and then I'll delete / adjust it.
If I understand what you're after correctly, then you don't want a formset for IncidentStatus, indeed perhaps you don't need a an IncidentStatus model at all. You seem to have about 10-15 different choices for status, so you could just use choices in your Incident
model. Then you can use a formset, specifically, a modelformset, but not for IncidentStatus
, rather for Incident
. Then you will not need to iterate, the formset factory will create a new form for each object in the Incident model, with it's own form.
Answered By - raphael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.