How to traverse the two yuan dictionary in the Django template - django

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!

Related

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?

How to display multiple object data in html table in django template

I passed two queryset objects from views to template both have product data with its features now the main problem is how to show this both objects data simultaneously in HTML table?
table structure:
features1 | product1 features value 1 | product2 feature value 1
features2 | product1 features value 2 | product2 feature value 2
...
<tbody>
{% for a in product_data %}{% for b in product_data_2 %}
<tr class="row" style="line-height: 3">
{% if forloop.counter == forloop.parentloop.counter %}
{% for feature_data in a.product_features.all %}{% for feature_data_1 in b.product_features.all %}
<td class="col-lg-4 col-md-4 col-sm-4 col-4" style="text-align: left; font-weight: bold;">
{{ feature_data.feature.feature_name }}
</td>
<td class="col-lg-4 col-md-4 col-sm-4 col-4" style="text-align: center;">
{{ feature_data.product_feature_value }}
</td>
<td class="col-lg-4 col-md-4 col-sm-4 col-4" style="text-align: center;">
{{ feature_data_1.product_feature_value }}
</td>
{% endfor %}{% endfor %}
{% elif forloop.counter < forloop.parentloop.counter %}
something
{% elif forloop.parentloop.counter < forloop.counter %}
something
{% endif %}
</tr>
{% endfor %}{% endfor %}
</tbody>
please try to answer i tried but nothing work
Depending on your purpose you can use access by index and with:
{% with first_obj=product_data.0 %}
{% with second_obj=product_data.1 %}
...
{% for feature_data in first_obj.product_features.all %}
...
{% endfor %}
{% for feature_data in second_obj.product_features.all %}
...
{% endfor %}

Django Display a selection list

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

How to appear two times in the django template?

I have two times: {{maine.job_time}} and {{maine.transport_time}}, I can add them using {% load mathfilters%}. That works well.
I want to check now if the result of {{aine.job_time | add: maine.transport_time}} is equal to "3:00:00" displays OK. But my condition does not work.
Can you help me solve this problem?
Thank you
Here is my Django template:
{% extends 'blog/base.html' %}
{% load mathfilters %}
{% block content %}
{% if semain %}
<div class="container">
<table class="table table-bordered" >
<thead>
<tr>
<th> {{ dim }} </th>
</tr>
</thead>
<tr>
<td>
{% for maine in SemaineView %}
{% if maine.date|date:"D" == "dim" %}
<p class='postcontent' ><strong>job:</strong> {{ maine.job_time}}</p>
<p class='postcontent' ><strong>transport:</strong> {{ maine.transport_time }}</p>
<p class='postcontent' ><strong>Total:</strong> {{ maine.job_time|add:maine.transport_time }}</p>
{% if maine.job_time|add:maine.transport_time== "3:00:00" %}
<h1> ok </h1>
{% else %}
<h1> Non </h1>
{% endif %}
{% endif %}
{% endfor %}
{{ tr }}
</td>
</tr>
</table>
</div>
{% endif %}
{% endblock %}

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 }}