Django list of item did not appear - python

I created model, view and template for display list of items in template but after I run the website It didn't anything. Please help me to solve this.
model.py
class waterLevel(models.Model):
height = models.CharField(max_length=250)
date = DateTimeField(max_length=250)
def __str__(self):
# #
return self.height + '-' + self.date
view.py
def compare(request):
all_waterLevels = waterLevel.objects.all()
context = {'all_waterLevels': all_waterLevels}
return render(request, 'Home/compare.html', context)
compare.html
{% if all_waterLevels %}
<ul>
{% for height in all_waterLevels %}
<li> {{ waterLevel.height }}</li>
<li> {{ waterLevel.date }}</li>
{% endfor %}
</ul>
{% else %}
<h3>no any details</h3>
{% endif %}

Try this :
{% if all_waterLevels %}
<ul>
{% for waterLevel in all_waterLevels %}
<li> {{ waterLevel.height }}</li>
<li> {{ waterLevel.date }}</li>
{% endfor %}
</ul>
{% else %}
<h3>no any details</h3>
{% endif %}

Related

Jinja template groupby sorting issue

In this code block, I have grouped by headings. But I want to sort the titles in array index order. Not alphabetically.
{% set list = widget.attributes.faq_item %}
{% for title_group in list|groupby('value.main_title') %}
<h2 class="account-sss__title">{{title_group.grouper}}</h2>
{% for item in title_group.list %}
<a href="#" class="account-sss__list--link js-link">
{{item.value.question}}
</a>
<div class="account-sss__content js-account-sss__content">
{{item.value.answer}}
</div>
{% endfor %}
{% endfor %}
I solved the problem.
{% set list = widget.attributes.faq_item %}
{% set Arr_titles = [] %}
{% for event in list %}
{% if event.value.main_title not in Arr_titles %}
{% do Arr_titles.append(event.value.main_title) %}
{% endif %}
{% endfor %}
{% for index in Arr_titles %}
<h2 class="account-sss__title">{{index}}</h2>
{% for item in list %}
{% if index == item.value.main_title %}
<a href="#" class="account-sss__list--link js-link">
{{item.value.question}}
</a>
<div class="account-sss__content js-account-sss__content">
{{item.value.answer}}
</div>
{% endif %}
{% endfor %}
{% endfor %}

Python Django Subcategory

I want to print the subcategory names of each Main Category. The below code is now showing all subcategories for every Main category. How can I do?
index.html
{% for mainCatList in main_cat_list %}
<li class="subMenu"><a>{{ mainCatList.main_category_name }}</a>
<ul>
{% for subCat in cat_list %}
<li><i class="icon-chevron-right"></i>{{ subCat.category_name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
views.html
from django.shortcuts import render
from .models import Product, Category, Main_Category
def homePageView(request):
main_cat_list = Main_Category.objects.all()
cat_list = Category.objects.all()
context = {'main_cat_list': main_cat_list, 'cat_list': cat_list}
return render(request, 'index.html', context)
I assume that your Category has foreign key to Main_Catagory. In that case, you can do this
{% for main_cat in main_cat_list %}
<li class="subMenu"><a>{{ main_cat.main_category_name }}</a>
<ul>
{% for sub_cat in main_cat.category_set.all %}
<li><i class="icon-chevron-right"></i>{{ sub_cat.category_name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}

django sort dict after query

have a table with websites and a many to one table with descriptions
trying to get a list, firstly getting the latest descriptions and then displaying them ordered by the content of the descriptions...
have the following in views.py
def category(request, category_name_slug):
"""Category Page"""
context_dict = {}
try:
category = Category.objects.get(slug=category_name_slug)
subcategory = SubCategory.objects.filter(category=category)
websites = Website.objects.filter(sub_categories=subcategory, online=True, banned=False)
sites = websites
descriptions = WebsiteDescription.objects.prefetch_related("about")
descriptions = descriptions.filter(about__in=sites)
descriptions = descriptions.order_by('about', '-updated')
descs = []
last_site = "" # The latest site selected
# Select the first (the latest) from each site group
for desc in descriptions:
if last_site != desc.about.id:
last_site = desc.about.id
desc.url = desc.about.url
desc.hs_id = desc.about.id
desc.banned = desc.about.banned
desc.referral = desc.about.referral
descs.append(desc)
context_dict['descs'] = descs
context_dict['websites'] = websites
context_dict['subcategory'] = subcategory
context_dict['category'] = category
except SubCategory.DoesNotExist:
pass
return render(request, 'category.html', context_dict)
this gives me a list with sites and their latest descriptions, so i have the following in category.html
{% if category %}
<h1>{{ category.name }}</h1>
{% for subcategory in category.subcategory_set.all %}
{{ subcategory.name }} ({{ subcategory.website_set.all|length }})
{% endfor %}
{% if descs %}
{% load endless %}
{% paginate descs %}
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo" %}
<ul id='list' class='linksteps'>
<a href="/{{ desc.about_id }}" rel="nofollow" target="_blank">
<img src="/static/screenshots/{{ desc.about_id }}.png" />
</a>
<li><h3>{{ desc.about_id }}{% if desc.title %} - {{ desc.title }} {% endif %}</h3>
{% if desc.description %}<b>Description: </b>{{ desc.description }}
<br />{% endif %} {% if desc.subject %}<b>Keywords: </b>{{ desc.subject }}
<br />{% endif %} {% if desc.type %}<b>Type: </b>{{ desc.type }}
<br />{% endif %} {% if desc.officialInfo %} {% if desc.language %}<b>Language: </b>{{ desc.language }}
<br />{% endif %} {% if desc.contactInformation %}<b>Contact info: </b>{{ desc.contactInformation }}
<br />{% endif %}
{% else %}
{% endif %}
</li>
</ul>
</div>
{% endfor %}
{% show_pages %}
{% else %}
<strong>No websites currently in category.</strong>
{% endif %}
{% else %}
The specified subcategory {{ category_name }} does not exist!
{% endif %}
Initially i used dictsort
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo"|dictsortreversed:"referral" %}
to give me the list in the desired order, so i was all happy ;)
Then however i decided i needed some pagination because the lists became too long.
django-endless-pagination works fine and does what its supposed too, however it splits up my list before the dictsort kicks in.
is there a way to sort before pagination happens and after i ordered_by at the initial query to have the latest descriptions selected?
much obliged
EDIT:
not getting any answers so my question might not be clear.
as far as i understand i need to sort the values in context_dict at the end in views.py replacing the dictsort as in the template
SOLVED:::
doing this did the trick for me to replace the dictsort.
descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1
SOLVED:::
doing this did the trick for me to replace the dictsort.
descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1

Paginating based on the alphabet (A-Z)

I have a simple contacts application where I want to paginate the contact list based on first name alphabet.
Here is my views.py
def contacts(request):
contact_list = Contact.objects.filter(user=request.user).order_by('first_name')
pages = [contact_list.filter(first_name__istartswith=i) for i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
return render(request, 'contact/contact.html', {'contact_list': contact_list, 'pages': pages})
Here is my template
{% for alphabet in pages %}
{% if alphabet %}
<p>{{ alphabet }}</p>
{% for contact in contact_list %}
{% if contact in pages %}
<ul>
<li>{{ contact.first_name }}</li>
</ul>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
The output from this is something like
[<Contact: Steve>, <Contact: Su>]
Steve
Su
[<Contact: Ted>]
Ted
[<Contact: Zedd>]
Zedd
The {{ alphabet }} prints a list. What should I write to print the alphabet instead?
Capture the alphabet in the context variable:
pages = [{"letter": i, "contacts": contact_list.filter(first_name__istartswith=i) }
for i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
return render(request, 'contact/contact.html', {'pages': pages}) # why pass contact_list?
Your template should be simplified to:
{% for alphabet in pages %}
{% if alphabet.contacts %}
<p>{{ alphabet.letter }}</p>
{% for contact in alphabet.contacts %}
<ul>
<li>{{ contact.first_name }}</li>
</ul>
{% endfor %}
{% endif %}
{% endfor %}

divide a categorized list into parts in django template

I have this model:
class Event_Category(models.Model):
event=models.ForeignKey(Event,related_name='event_category')
category=models.ForeignKey(Category,related_name='events')
user=models.ForeignKey(User)
in the view:
magazie_cats=Event_Category.objects.filter(event=instance).order_by('category').distinct()
return render_to_response('CompanyHub/Company/index.html', {'magazie_cats':magazie_cats},context_instance=RequestContext(request))
in the template:
{% regroup magazie_cats by category as service_list %}
I want to divide this categorized list into 3 parts and iterate over it. I tried to access each category by variable indexes:
{% for i in range(0,3) %}
{% for item in service_list.i.list %}
{{item.event.title}}
{% endfor %}
{% endfor %}
{% for i in range(3,6) %}
{% for item in service_list.i.list %}
{{item.event.title}}
{% endfor %}
{% endfor %}
{% for i in range(6,9) %}
{% for item in service_list.i.list %}
{{item.event.title}}
{% endfor %}
{% endfor %}
NOTE: the range for each loop is calculated by some custom filters based on service_list length . I didn't include the complete code to avoid complexity.
The problem is that the list variable index doesn't work and I don't know what to do.
You want to use the slice filter:
{% regroup magazie_cats by category as service_list %}
{% for cat in service_list|slice:":3" %}
{% for item in cat.list %}
{{item.event.title}}
{% endfor %}
{% endfor %}
{% for cat in service_list|slice:"3:6" %}
{% for item in cat.list %}
{{item.event.title}}
{% endfor %}
{% endfor %}
{% for cat in service_list|slice:"6:9" %}
{% for item in cat.list %}
{{item.event.title}}
{% endfor %}
{% endfor %}

Categories

Resources