Django Display a selection list - django

I am trying to implement a form where candidates can be selected for approval processing. I want to display a list of candidates, so their data can be viewed during selection.
models.py
class SubmissionApproval(models.Model):
candidate=models.ForeignKey(Candidate,on_delete=models.CASCADE)
clientProcess=models.ForeignKey(ClientProcessDetails,verbose_name="Client
Process",help_text="Process",on_delete=models.PROTECT)
dateSubmitted=models.DateTimeField(auto_now_add=True)
comments=models.CharField("Comments",max_length=200,blank=True,null=True)
class Meta:
verbose_name="First Stage Approval"
unique_together=(('candidate','clientProcess'),)
def __str__(self):
return self.candidate.name
views.py
def submissionApprovalList(request):
object_list=Candidate.objects.all()
if request.method=="POST":
fm=SubmissionApprovalFormList(request.POST)
print("Reached Post")
if fm.is_valid():
print("Printing Candidates Start")
for item in fm.cleaned_data['candidates']:
print(item)
print("Printing candidates End")
else:
fm=SubmissionApprovalFormList()
return render(request,'itrecruitment/firstStageSubmissionList.html',{'form':fm,'object_list':object_list })
forms.py
class SubmissionApprovalFormList(forms.Form):
candidates=forms.ModelMultipleChoiceField(
queryset=Candidate.objects.all(), widget=forms.CheckboxSelectMultiple
)
template
{% extends "seekgeek/base.html" %}
{% block content %}
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Select Candidates for First Stage Submission</h3>
<button type="button" class="btn btn-primary pull-right">Add Candidate </button>
</div>
<!-- /. box-header -->
<div class="box-body table-responsive">
<!-- form details -->
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
{% bootstrap_messages %}
<form method="post" enctype="multipart/form-data" >
{% csrf_token %}
<table class="table table-bordered table-hover datatable2">
<thead>
<tr>
<td><b>Select</b></td>
<td><b>Candidate No</b></td>
<td><b>Candidate Name</b></td>
<td><b>Current CTC (LPA)</b></td>
<td><b>Expected CTC (LPA)</b></td>
<td><b>Experience in Yrs</b></td>
<td><b>Recruiter</b></td>
<td><b>Latest Status</b></td>
<td><b>Languages</b></td>
<td><b>Updated</b></td>
</tr>
</thead>
<!-- ./ table head -->
<tbody>
{% for obj in object_list %}
<tr>
<!--Select field for Form -->
<td> {{ form.candidates.0 }} </td>
<!--Select field for Form -->
<td>{{ obj.id }}</td>
<td>{{ obj.name }}</td>
<td>{{ obj.currentCTC }}</td>
<td>{{ obj.expectedCTC }}</td>
<td>{{ obj.experience }}</td>
<td>{{ obj.recruiter }}</td>
<td>{% if obj.latestRecStatus %}
<p><a class='text-green' href="{{ obj.latestRecStatus.get_absolute_url }}">{{ obj.latestRecStatus }}</a></p>
<p><a class='text-yellow' href="{% url 'clients:process-detail' obj.latestRecStatus.clientProcess.id %}"> {{ obj.latestRecStatus.clientProcess }} </a></p>
{% else %}
<p>No Status</p>
{% endif %}
</td>
<td>
{% for item in obj.programmingLanguages.all %}
<p>{{ item }}</p>
{% endfor %}
</td>
<td>{{ obj.updated }}</td>
</tr>
{% endfor %}
</tbody>
<!-- /. table body -->
</table>
<!-- /. table -->
{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
</div>
<!-- /. box body -->
</div>
<!-- /. box -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
{% endblock %}
What is the correct way of doing this. intention is similar to change list form in django admin

Related

Pagination doesn`t work with extra context in Django ListView

I am trying to create a simple pagination through a query of filtered instances of Need model.
The problem is that pagination doesn`t work at all, and I guess this is because of extra content.
Here is my current view, which shows pagination on front-end as an empty div:
class CategoryNeeds(ListView):
model = Need
template_name = "volunteering/category_needs.html"
paginate_by = 1
context_object_name = "needs"
def get_queryset(self):
return Need.objects.filter(category__name=self.kwargs["name"].capitalize())
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
category = Category.objects.get(name=kwargs["name"].capitalize())
self.extra_context = {
"category": category,
"needs": self.object_list
}
return self.render_to_response(self.extra_context)
And here is the template:
{% extends "index/index.html" %}
{% block content %}
<section class="contact-section bg-black">
<div class="container px-4 px-lg-5">
<div class="row gx-4 gx-lg-5">
<div class="col-md-4 mb-3 mb-md-0">
<div class="card-body text-center">
<h1>{{ category.name }}</h1>
<hr style="size: A5">
</div>
</div>
</div>
</div>
</section>
<section class="about-section text-center" id="about">
<div class="container px-4 px-lg-5">
<h2>Needs:</h2>
<table class="table table-dark table-striped">
<thead>
<tr>
<th scope="col">Photo</th>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Price</th>
<th scope="col">Donation</th>
<th scope="col">City</th>
{% if user.pk == user_page.pk %}
<th scope="col">Update</th>
<th scope="col">Delete</th>
{% endif %}
</tr>
</thead>
<tbody>
{% for need in needs %}
<tr data-href="{% url "volunteering:need" need.pk %}">
<td>{% if need.photo %}<img src="{{ need.photo.url }}">{% endif %}</td>
<td>{{ need.title }}</td>
<td>{{ need.description|truncatechars:10 }}</td>
<td>{{ need.price }}</td>
<td>{{ need.donation }}</td>
<td>{{ need.city }}</td>
{% if user.pk == user_page.pk %}
<td>Update</td>
<td>Delete</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div>
{% if page_obj.has_previous %}
« Previous page
{% if page_obj.number > 3 %}
1
{% if page_obj.number > 4 %}
<span>...</span>
{% endif %}
{% endif %}
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
{{ num }}
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
{{ num }}
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
{% if page_obj.number < page_obj.paginator.num_pages|add:'-3' %}
<span>...</span>
{{ page_obj.paginator.num_pages }}
{% elif page_obj.number < page_obj.paginator.num_pages|add:'-2' %}
{{ page_obj.paginator.num_pages }}
{% endif %}
Next Page »
{% endif %}
</div>
</section>
{% endblock %}
The reason why I think the problem lies in extra content is because this view works perfectly with pagination:
class AllNeeds(ListView):
model = Need
context_object_name = "needs"
paginate_by = 3
Could someone explain, why doesn`t my pagination work, please?
Would be very grateful for all your responce!
Yep, it seems that you are overiding normal django flow which adds context, try this:
class CategoryNeeds(ListView):
model = Need
template_name = "volunteering/category_needs.html"
paginate_by = 1
def get_queryset(self):
return Need.objects.filter(category__name=self.kwargs["name"].capitalize())
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
category = Category.objects.get(name=kwargs["name"].capitalize())
context = self.get_context_data()
context['category'] = category
return self.render_to_response(context)

Form Filter with Pagination not working in Django

I am trying to use Form filters and Pagination together in a view and only one works at one point of time, I am not able to make both work together, Please help if there is a workaround to my problem.
views.py
`
def viewAmc(request):
amc = Amc.objects.all()
paginator = Paginator(amc, 10)
page_obj = paginator.page(request.GET.get('page', '1'))
amcFilter = AmcFilter(request.GET, queryset=amc)
amc = amcFilter.qs#qs means query set
context = {'amc': page_obj,
'amcFilter': amcFilter}
return render(request, 'view_amc.html', context)
`
filters.py
`
class AmcFilter(django_filters.FilterSet):
class Meta:
model = Amc
fields = 'amc_type', 'amc_status'
`
view_amc.html
`
{% extends 'base.html' %}
{% load static %}
{% block content %}
<br>
<div class="row">
<div class="col-md-8">
<div class="card card-body">
<h5> Annual Maintenance Contract :</h5>
<div class="card card-body">
<form method="get">
{{ amcFilter.form.amc_type.label_tag }}{{ amcFilter.form.amc_type }}
{{ amcFilter.form.amc_status.label_tag }}{{ amcFilter.form.amc_status }}
<button class="btn btn-primary" type="submit">Filter</button>
</form>
</div>
<div class="col">
</div>
<div class="card card-body">
<a class="btn btn-primary btn-sm btn-block" href="{% url 'create_amc' %}">Create New AMC</a>
<table class="table table-sm">
<tr>
<th>Customer</th>
<th>Location</th>
<th>AMC ID</th>
<th>AMC Type</th>
<th>AMC Status</th>
<th>AMC Cost</th>
<th>AMC Start</th>
<th>AMC Expiry</th>
</tr>
{% for items in amc %}
<tr>
<td>{{ items.customer }}</td>
<td>{{ items.location }}</td>
<td>{{ items.name }}</td>
<td>{{ items.amc_type }}</td>
<td>{{ items.amc_status }}</td>
<td>{{ items.amc_cost }}</td>
<td>{{ items.start_date }}</td>
<td>{{ items.end_date }}</td>
{% endfor %}
</tr>
</table>
</div>
<nav><div class="paginationDiv">
<ul class="pagination" >
{% if amc.has_previous %}
<li>
<a href="?page={{amc.previous_page_number}}" aria-label="Previous">
<span aria-hidden="true"><b>Prev</b></span>
</a>
</li>
{% endif %}
{% if amc.paginator.num_pages > 1 %}
{% for p in amc.paginator.page_range %}
<li class="{% if amc.number == p %}active{% endif %}"><b>{{p}}</b></li>
{% endfor %}
{% if amc.has_next %}
<li>
<a href="?page={{amc.next_page_number}}" aria-label="Next">
<span aria-hidden="true"><b>Next</b></span>
</a>
</li>
{% endif %}
{% endif %}
</ul>
</div>
</nav>
</div>
</div>
</div>
{% endblock %}
`
Now, Either I can make Paginator to use the object 'amc' in the context by passing it to 'page_obj' or changing to 'amc' will make Form filters to work, cant achieve both together. Please show me a work around for this?

Django - update inline formset not updating

I'm trying to create an update view which has a few inline formset's in it but for some reason I'm seeing 'id: This field is required.' & none of the inline formset values being set when I click the update button. The fields themselves actually have values in them in the template so I'm not sure why the inline formsets would be empty when trying to save.
Models.py
class ProjectUpdateForm(forms.ModelForm):
class Meta:
model = Project
fields = ('name', 'details')
Views.py
class ProjectUpdateView(LoginRequiredMixin, UpdateView):
model = Project
template_name = 'home/project/project_update.html'
context_object_name = "project"
form_class = ProjectUpdateForm
def get_context_data(self, **kwargs):
ReviewChildFormset = inlineformset_factory(
Project, AdoptedBudgetReview, fields=('project', 'review'), can_delete=False, extra=0
)
itemNames = [{'item': item} for item in ['Concept & Feasibility', 'Planning & Design', 'Procurement', 'Delivery & Construction', 'Finalisation']]
EstimatedBudgetChildFormset = inlineformset_factory(
Project, EstimatedBudget, fields=('project', 'item', 'cost', 'time'), can_delete=False, formset=EstimatedInlineFormset, extra=0, widgets={'item': forms.Select(attrs={'disabled': True})},
)
FutureExpenditureFormset = inlineformset_factory(
Project, FutureExpenditure, fields=('project', 'byear', 'internalFunding', 'externalFundingSource', 'externalFunding'), can_delete=False, extra=0,
)
PreviousExpenditureFormset = inlineformset_factory(
Project, PreviousExpenditure, fields=('project', 'byear', 'internalFunding', 'externalFunding'), can_delete=False, extra=0,
)
initial = [{'priority': priority} for priority in Priority.objects.all()]
PriorityChildFormset = inlineformset_factory(
Project, ProjectPriority, fields=('project', 'priority', 'score'), can_delete=False, extra=0, widgets={'priority': forms.Select(attrs={'disabled': True})},
)
context = super().get_context_data(**kwargs)
if self.request.POST:
context['adoptedreviews'] = ReviewChildFormset(self.request.POST, instance=self.object)
context['estimatedbudget'] = EstimatedBudgetChildFormset(self.request.POST, initial=itemNames, instance=self.object)
context['futureexpenditure'] = FutureExpenditureFormset(self.request.POST, instance=self.object)
context['previousexpenditure'] = PreviousExpenditureFormset(self.request.POST, instance=self.object)
context['priorities'] = PriorityChildFormset(self.request.POST, initial=initial, instance=self.object)
else:
context['adoptedreviews'] = ReviewChildFormset(instance=self.object)
context['estimatedbudget'] = EstimatedBudgetChildFormset(initial=itemNames, instance=self.object)
context['futureexpenditure'] = FutureExpenditureFormset(instance=self.object)
context['previousexpenditure'] = PreviousExpenditureFormset(instance=self.object)
context['priorities'] = PriorityChildFormset(initial=initial, instance=self.object)
return context
def form_valid(self, form):
context = self.get_context_data()
adoptedreview = context["adoptedreviews"]
estimatedbudget = context["estimatedbudget"]
prioritycriteria = context["priorities"]
futureexpenditure = context["futureexpenditure"]
previousexpenditure = context["previousexpenditure"]
form.instance.creator = self.request.user
if adoptedreview.is_valid() and estimatedbudget.is_valid() and previousexpenditure.is_valid() and futureexpenditure.is_valid and prioritycriteria.is_valid():
self.object = form.save()
adoptedreview.instance = self.object
estimatedbudget.instance = self.object
futureexpenditure.instance = self.object
previousexpenditure.instance = self.object
prioritycriteria.instance = self.object
adoptedreview.save()
estimatedbudget.save()
previousexpenditure.save()
futureexpenditure.save()
prioritycriteria.save()
else:
return self.form_invalid(form)
return super(ProjectCreateview, self).form_valid(form)
Template
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Update Project</legend>
{{ form|crispy }}
</fieldset>
<h2>Adopted Budget Review</h2>
<!-- {{ adoptedreviews|crispy }} -->
<table class="table table-light">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">BR1</th>
<th scope="col">BR2</th>
<th scope="col">BR3</th>
<th scope="col">BR4</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Amount</th>
{{ adoptedreviews.management_form }}
{% for adrform in adoptedreviews.forms %}
{% for field in adrform.visible_fields %}
<td>
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
{% endfor %}
</tr>
</tbody>
</table>
<h2>Estimated Budget - Breakdown Yr1</h2>
<!-- {{ estimatedbudget|crispy }} -->
<table class="table table-light">
{{ estimatedbudget.management_form }}
<thead>
<tr>
<th scope="col">Item - Description</th>
<th scope="col">Anticipated % cost</th>
<th scope="col">Anticipated time - weeks</th>
</tr>
</thead>
{% for form in estimatedbudget.forms %}
<tr>
<td>
{{ form.errors }}
<h4>{{ form.item.value }}</h4>
{{ form.item.as_hidden }}
</td>
<td>
{{ form.cost }}
</td>
<td>
{{ form.time }}
</td>
</tr>
{% endfor %}
</table>
<h2>Future Project Expenditure</h2>
<!-- {{ futureexpenditure|crispy }} -->
<table class="table table-light">
{{ futureexpenditure.management_form }}
{% for form in futureexpenditure.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th scope="col">{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tbody>
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
</tbody>
{% endfor %}
</table>
<h2>Previous Project Expenditure</h2>
<table class="table table-light">
<!-- {{ previousexpenditure|crispy }} -->
{{ previousexpenditure.management_form }}
{% for form in previousexpenditure.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th scope="col">{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tbody>
<tr class="{% cycle row1 row2 %} previous_formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
</tbody>
{% endfor %}
</table>
<h2>Priority Criteria</h2>
<!-- {{ priorities|crispy }} -->
<table class="table table-light">
<tbody>
{{ priorities.management_form }}
{% for priority in priorities.forms %}
<tr>
<td>
{{ priority.priority.errors.as_ul }}
{% for value, name in priority.priority.field.choices %}
{% if value == priority.priority.value %}
<h4>{{ name }}</h4>
{% endif %}
{% endfor %}
{{ priority.priority.as_hidden }}
</td>
<td>
{{ priority.score.errors.as_ul }}
{{ priority.score }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Update Project</button>
</div>
</form>
Whenever one renders a forms manually one should remember to always render it's hidden fields, more so for a formset / inlineformset since it automatically adds some hidden fields to the form. Since the formset is updating multiple instances of course there needs to be something present in the formset to indicate which instance is which, which is done by hidden fields here.
Hence you need to render the hidden fields of your formsets forms:
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Update Project</legend>
{{ form|crispy }}
</fieldset>
<h2>Adopted Budget Review</h2>
<!-- {{ adoptedreviews|crispy }} -->
<table class="table table-light">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">BR1</th>
<th scope="col">BR2</th>
<th scope="col">BR3</th>
<th scope="col">BR4</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Amount</th>
{{ adoptedreviews.management_form }}
{% for adrform in adoptedreviews.forms %}
{% for hidden in adrform.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in adrform.visible_fields %}
<td>
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
{% endfor %}
</tr>
</tbody>
</table>
<h2>Estimated Budget - Breakdown Yr1</h2>
<!-- {{ estimatedbudget|crispy }} -->
<table class="table table-light">
{{ estimatedbudget.management_form }}
<thead>
<tr>
<th scope="col">Item - Description</th>
<th scope="col">Anticipated % cost</th>
<th scope="col">Anticipated time - weeks</th>
</tr>
</thead>
{% for form in estimatedbudget.forms %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<tr>
<td>
{{ form.errors }}
<h4>{{ form.item.value }}</h4>
{{ form.item.as_hidden }}
</td>
<td>
{{ form.cost }}
</td>
<td>
{{ form.time }}
</td>
</tr>
{% endfor %}
</table>
<h2>Future Project Expenditure</h2>
<!-- {{ futureexpenditure|crispy }} -->
<table class="table table-light">
{{ futureexpenditure.management_form }}
{% for form in futureexpenditure.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th scope="col">{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tbody>
<tr class="{% cycle row1 row2 %} formset_row">
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
</tbody>
{% endfor %}
</table>
<h2>Previous Project Expenditure</h2>
<table class="table table-light">
<!-- {{ previousexpenditure|crispy }} -->
{{ previousexpenditure.management_form }}
{% for form in previousexpenditure.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th scope="col">{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tbody>
<tr class="{% cycle row1 row2 %} previous_formset_row">
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
</tbody>
{% endfor %}
</table>
<h2>Priority Criteria</h2>
<!-- {{ priorities|crispy }} -->
<table class="table table-light">
<tbody>
{{ priorities.management_form }}
{% for priority in priorities.forms %}
{% for hidden in priority.hidden_fields %}
{{ hidden }}
{% endfor %}
<tr>
<td>
{{ priority.priority.errors.as_ul }}
{% for value, name in priority.priority.field.choices %}
{% if value == priority.priority.value %}
<h4>{{ name }}</h4>
{% endif %}
{% endfor %}
{{ priority.priority.as_hidden }}
</td>
<td>
{{ priority.score.errors.as_ul }}
{{ priority.score }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Update Project</button>
</div>
</form>
Also since you have multiple formsets you should provide a prefix to them to provide name clashing (See Using more than one formset in a view):
if self.request.POST:
context['adoptedreviews'] = ReviewChildFormset(self.request.POST, instance=self.object, prefix='adoptedreviews_form')
context['estimatedbudget'] = EstimatedBudgetChildFormset(self.request.POST, initial=itemNames, instance=self.object, prefix='estimatedbudget_form')
context['futureexpenditure'] = FutureExpenditureFormset(self.request.POST, instance=self.object, prefix='futureexpenditure_form')
context['previousexpenditure'] = PreviousExpenditureFormset(self.request.POST, instance=self.object, prefix='previousexpenditure_form')
context['priorities'] = PriorityChildFormset(self.request.POST, initial=initial, instance=self.object, prefix='priorities_form')
else:
context['adoptedreviews'] = ReviewChildFormset(instance=self.object, prefix='adoptedreviews_form')
context['estimatedbudget'] = EstimatedBudgetChildFormset(initial=itemNames, instance=self.object, prefix='estimatedbudget_form')
context['futureexpenditure'] = FutureExpenditureFormset(instance=self.object, prefix='futureexpenditure_form')
context['previousexpenditure'] = PreviousExpenditureFormset(instance=self.object, prefix='previousexpenditure_form')
context['priorities'] = PriorityChildFormset(initial=initial, instance=self.object, prefix='priorities_form')
Note: You seem to have weird HTML (multiple tbody elements in a table??), and also you might want to reconsider having so many
formsets in a page, this can be considered bad for UX purposes (This
can be really confusing for the user, not to mention the developer
too). One page should ideally serve one purpose to the user.

django template table rows do not collapse

I can not work-out what I am doing wrong with collapsing certain rows of my table.
Can you advice how can I correct it so I can collapse rows marked in red ?
I guess I have placed collapse tag in wrong place or have not used again ? However can I use table in table ?
my template:
{% extends "base.html" %}
{% block content %}
<h3>{% for project in projects %}{{ project.project_name }}{% endfor %}</h3>
<table class="table table-striped table table-bordered table table-hover table table-condensed">
<tr>
<td></td>
<!-- row with zone names -->
{% for z in zones %}
<td style="width:40px" align="center">{{ z.zone_name }}</td>
{% endfor %}
</tr>
<!-- rows with stage names -->
{% for trstage in trstages %}
<tr>
<td style="width:40px"><a data-toggle="collapse" href="#collapse{{ trstage }}">{{ trstage.stage_name }}</a></td>
<!-- row with stage value -->
{% for stage in trstage.stages %}
{% for zone in zones|dictsort:"zone_name" %}
{% if zone == stage.zone %}
<td style="width:40px" align="center">{{ stage.substage_sum.sum }}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
<div id="collapse{{ trstage }}" class="panel-collapse collapse">
<!-- rows with substages names -->
{% for trsubstage in trstage.related_trsubstages %}
<tr>
<td>{{ trsubstage.substage_name }}</td>
<!-- row with substage value -->
{% for substage in trsubstage.substages_related %}
{% for zone in zones %}
{% if zone == substage.stage.zone %}
<td style="width:40px" align="center">{{ substage.substage_value }}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
</div>
{% endfor %}
</table>
{% endblock %}
Below is link to my current template view:
Try adding the id of your trstage to the href and id of the divs. I did something similar in my project and it worked. I hope it works for you too. Like this,
<div id="{{ trstage.id.value }}" class="panel-collapse collapse">
and
<a data-toggle="collapse" href="#{{ trstage.id.value }}">{{ trstage.stage_name }}

How to traverse the two yuan dictionary in the Django template

1.my view have a dict,For example:
category_all = {
u'Python': {
'acticle_info': [
[<Article: python_tump>],[<Article:python_dict>]
],
'article_count': 22
}
}
2.my render is:
return render(request, 'blog/index.html',{'category_all':category_all})
3.i traverse the dict in Django template:
<!-- tabs Nav -->
<ul id="myTab" class="nav nav-tabs" role="tablist">
{% for blog_category in category_all.keys %}
<li role="presentation">{{ blog_category }}</li>
{% endfor %}
</ul>
<!-- Tab panes -->
<div id="myTabContent" class="tab-content">
{% for blog_category,blog_info in category_all.items %}
<div role="tabpanel" class="tab-pane" id="{{ blog_category }}">
<table class="table table-hover">
<tbody>
{% for get_blog_info in blog_info.acticle_info %}
{% for blog_result in get_blog_info %}
<tr class="active">
<td>{{ blog_result.title }}</td>
<td>{{ blog_result.date_publish }}</td>
</tr>
{% endfor get_blog_info %}
{% endfor %}
</tbody>
</table>
</div>
% endfor %}
</div>
4.help
4.1.How to get the article_count value
4.2.I used a lot of for loop, how can reduce the for cycle
You can get article_count value by:
blog_info.article_count
Change your category_all structure for reduce loops!