Cant write correct queryset in Django - django

i have three models:subplan,subplanfeature and price.I wanna show gym price table,but it shows wrong result
class SubPlan(models.Model):
title = models.CharField(max_length=150)
def __str__(self):
return self.title
class SubPlanFeature(models.Model):
title = models.CharField(max_length=150)
def __str__(self):
return self.title
class Price(models.Model):
gym=models.ForeignKey(Gym,on_delete=CASCADE)
subplan=models.ForeignKey(SubPlan,on_delete=CASCADE,related_name='subplans')
feature=models.ForeignKey('SubPlanFeature',on_delete=CASCADE,related_name='features')
price = models.IntegerField()
highlight_status = models.BooleanField(default=False,null=True)
here is my view
def pricing(request,slug):
gym=models.Gym.objects.get(slug=slug)
price=models.Price.objects.all()
banners=models.Banners.objects.filter(gym=gym)
plans1= models.Price.objects.filter(gender='man',gym=gym)
dfeatures1=models.Price.objects.filter(gender='man',gym=gym)
return render(request, 'pricing.html',{'plans1':plans1,'dfeatures1':dfeatures1,'gym':gym})
and template here
<thead>
<tr>
<th style="width: 34%;"></th>
{% for plan in plans1 %}
<th style="width: 22%;">{{plan.subplan}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for feature in dfeatures1 %}
<tr>
<th scope="row" class="text-start">{{feature.feature}}</th>
{% for plan in plans1 %}
<td>
{{plan.price}} Azn.
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
it should display as this
enter image description here
but shows this
enter image description here
what should i change?

you can use "forloop" counter in template
...
{% for feature in dfeatures1 %}
<tr>
<th scope="row" class="text-start">{{feature.feature}}</th>
{% for plan in plans1 %}
<td>
# only print when counter in parent loop are the same
{% if forloop.parentloop.counter == forloop.counter %}
{{plan.price}} Azn.
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}

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)

Outputting a table to a template

I recently began to study Django and cannot understand how to correctly implement the template (table)
I have a model:
She has ties to Company and bank.
class BankAccount(models.Model):
company = models.ForeignKey('Company.Company', null=True, on_delete=models.CASCADE, verbose_name='Фирма',
related_name='company')
bank = models.ForeignKey('Bank', null=True, on_delete=models.CASCADE, verbose_name='Банк', related_name='bank')
login_bank = models.CharField(max_length=255, verbose_name='Логин', null=False)
password_bank = models.CharField(max_length=255, verbose_name='Пароль', null=False)
date_created = models.DateTimeField(auto_now=True)
date_updated = models.DateTimeField(auto_now_add=True)
balance = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)
In the template I want to display a table where all firms / banks will be displayed at the intersection of these columns and rows, a value from this model will appear.
I've been looking for an answer for 5 days and
Company1
Company2
Company3
Company4
Bank1
Balance(Company1/Bank1)
Balance(Company2/Bank1)
Balance(Company/Bank1)
Balance(Company4/Bank1)
Bank2
Balance(Company1/Bank2)
....
....
....
Bank3
Balance(Company1/Bank3)
....
....
....
I tried the example template below, thanks
#Linh Nguyen
<table class="table table-bordered">
<tr>
<th> </th>
{% for company in companies %}
<th>{{ company.name }}</th>
{% endfor %}
</tr>
{% for bank in banks %}
<tr>
<td>{{bank.name}} </td>
{% for company in companies %}
{% for account in bank_accounts %}
{% if account.company_id == company.id and bank.id == account.bank_id %}
<td>{{account}}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
</table>
context = {
'companies': Company.objects.all().order_by('id'),
'banks': Bank.objects.all().order_by('id'),
'bank_accounts': BankAccount.objects.all()
}
But the result is still not desired. One firm can have 2 and 4 accounts in the table of accounts (bank_account).
and if the company does not have all 4 accounts, the data no longer match.If you add {% else%}, then the table breaks completely.
screen
<div class="col-md-12">
<div class="row">
<table class="table table-bordered">
<tr>
<th> </th>
{% for company in companies %}
<th>{{ company.name }}</th>
{% endfor %}
</tr>
{% for bank in banks %}
<tr>
<td>{{bank.name}} </td>
{% for company in companies %}
<td>
{% for account in bank_accounts %}
{% if account.company_id == company.id and bank.id == account.bank_id%}
{{account}}
{% endif %}
{% endfor %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
</div>
Finally it worked out!
I think it only come down to how you handle the logic in the looping:
in your view you have the querysets for all the banks, companies and bank accounts
view.py:
context = {
'companies': Company.objects.all(),
'banks': Bank.objects.all(),
'bank_accounts': BankAccount.objects.all()
}
template.html:
<table style="width:100%">
<tr>
{% for company in companies %}
<th>{{ company.name }}</th>
{% endfor %}
</tr>
{% for bank in banks %}
<tr>
{% for company in companies %}
{% for bank_account in bank_accounts %}
<td>
{% if bank_account.company == company %}
Balance({{ company.name }}/{{ bank.name }})
{% endif %}
</td>
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
</table>
First, you should implement your view function like this:
from django.shortcuts import render
from models import BackAccount
def get_bank_accounts(request):
accounts = BankAccount.objects.all()
context = {
'accounts': accounts,
}
return render(request, 'accounts.html', context)
So, that way you send all accounts to the template. And the way you use them in accounts.html like this:
{% for account in accounts %}
{{ account.bank }}
{{ account.company }}
{% endfor %}

raise self.RelatedObjectDoesNotExist( Portal.models.logger.Student.RelatedObjectDoesNotExist: logger has no Student

I keep getting this error everytime I try to access the class assignment page.
Below is the code of my project.
Models.py
class logger(AbstractUser):
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
class Student(models.Model):
username = models.OneToOneField(logger,on_delete=models.CASCADE,primary_key=True,related_name='Student')
name=models.CharField(max_length=250)
roll_no = models.CharField(max_length=50)
email = models.EmailField(max_length=254)
def __str__(self):
return self.name
class Meta:
ordering = ['roll_no']
class Teacher(models.Model):
username = models.OneToOneField(logger,on_delete=models.CASCADE,primary_key=True,related_name='Teacher')
name = models.CharField(max_length=250)
subject_name = models.CharField(max_length=250)
email = models.EmailField(max_length=254)
class_students = models.ManyToManyField(Student,through="StudentsInClass")
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('teacher_detail',kwargs={'pk':self.pk})
Views.py
#login_required
def class_assignment(request):
student = request.user.Student
assignment = SubmitAssignment.objects.filter(student=student)
assignment_list = [x.submitted_assignment for x in assignment]
return render(request,'class_assignment.html',{'student':student,'assignment_list':assignment_list})
class_assignment.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="jumbotron">
{% if student.student_assignment.count == 0 %}
<h2>No assignments Yet</h2>
{% else %}
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Assignment Name</th>
<th scope="col">Uploaded By</th>
<th scope="col">Uploaded Date</th>
<th scope="col">Download</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
{% for assignment in student.student_assignment.all %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
<td>{{ assignment.assignment_name }}</td>
<td>{{ assignment.teacher }}</td>
<td>{{ assignment.created_at }}</td>
<td>Download</td>
{% if assignment in assignment_list %}
<td>Submitted</td>
{% else %}
<td>Submit</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</div>
{% endblock %}
Can anyone please help me and tell me if something is wrong in the code or not, I have tried almost everything including syncing the database, trying to migrate the project, deleting and re-creating the database.
Any help would be appreciated, Thank you and if you need anything else please comment and I'll upload it.

How do I get a list of the categories column, and then list of everything in a single view?

I want a navbar on top of my page with the categories in my model.
And when I click on the link to a category, I want a table that lists the items in that category.
For whatever reason I just can't get both things to work at the same time.
This is my model
from django.db import models
# Create your models here.
class menulist(models.Model):
name = models.CharField(max_length=120)
description = models.CharField(max_length=500)
price = models.DecimalField(decimal_places=1, max_digits=10, default=100.00)
category_choices = (
('breads', 'Breads'),
('cakes', 'Cakes'),
('hotfood', 'Hot Food'),
('porkrolls', 'Pork Rolls'),
('drinks', 'Drinks'),
('MISC', 'Misc'),
)
category = models.CharField(max_length=50, choices=category_choices, default='MISC',)
dateadded = models.DateField(auto_now_add=True)
dateupdated = models.DateField(auto_now=True)
img = models.ImageField(upload_to='products/', default='products/blank.jpg')
def __str__(self):
return self.name
This is the view:
class ProductAdminView(ListView):
template_name = 'menulistapp/product_admin.html'
def get_context_data(self, *, object_list=None, **kwargs):
context = super(ProductAdminView, self).get_context_data(**kwargs)
categorylist = menulist._meta.get_field('category').choices
context = {
"category_list": categorylist,
}
return context
def get_queryset(self):
slug = self.kwargs.get("slug")
if slug:
queryset = menulist.objects.filter(category__iexact=slug)
else:
queryset = menulist.objects.all()
return queryset
html template:
{% extends 'base.html' %}
{% block content %}
<div class="container ">
<ul class="nav nav-pills border-bottom border-top pt-2 pb-2">
{% for a, b in category_list %}
<li class="nav-item">
<a class="nav-link" href="/productadmin/{{ a }}">{{ b }}</a>
</li>
{% endfor %}
</ul>
</div>
<!-- tables -->
<div class="container">
<div class="row">
<table class="table table-striped table-hover ">
<thead class="thead-dark">
<tr>
<th style="width: 15%"scope="col"></th>
<th style="width: 55%" scope="col">Product Name</th>
<th scope="col">Category</th>
<th scope="col">Price</th>
<th scope="col">Date Added</th>
<th scope="col">Last Updated</th>
</tr>
</thead>
<tbody>
{% for obj in object_list %}
<tr>
<td class="align-middle"><img src="{{ obj.img.url }}" class="img-fluid"></td>
<td class="align-middle">{{ obj }}</td>
<td class="align-middle">{{ obj.category }}</td>
<td class="align-middle">${{ obj.price }}</td>
<td class="align-middle">{{ obj.dateadded }}</td>
<td class="align-middle">{{ obj.dateupdated }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
Sometimes I can only get the list working if I comment out the nav bar, and vice versa.
I've looked at mixins, but that only confused me more and doesn't do anything.

Setting specific day to print specific message in the django template

Here is my model:
class Sitting(models.Model):
sit_date = models.DateField(blank=False)
cut_off_date = models.DateField(null=True, blank=True)
ballot_date = models.DateField(null=True, blank=True)
sess_no = models.ForeignKey(Session,
on_delete=models.CASCADE)
genre = TreeForeignKey('Genre', null=True, blank=True, db_index=True)
here is my view:
def sitting_list(request):
sitting=Sitting.objects.select_related('genre')
return render(
request,
'genre/sitting_list.html',
{'sittings':sitting, }, )
Here is my template:
{% block content %}
<table>
<tr>
<th>Sitting Day & Date</th>
<th>Ministry/Division</th>
<th>Ballot Date</th>
</tr>
{% for sitting in sittings %}
<tr>
<td> {{ sitting.sit_date|date:"l, d F, Y" }}</td>
{% for genre in sitting.genre.get_descendants %}
<td> {{ genre }},</td>
{% endfor %}
<td>(Ballot: {{ sitting.ballot_date}})</td>
</tr>
<tr>
</tr>
{% endfor %}
</table>
{% endblock %}
It gave following output:
Edit :
But I want following output:
You can see from my desired output, Ministry's name only come when no division exists. Moreover numbering and last full stop also not coming.
I hope this can also be fixed with your help.
You just need to add an if statement to see if the weekday is 2 (0-6 mon-sun)
<td>
{% if sitting.sit_date.weekday == 2 %}
extra stuff
{% endif %}
{% for genre in sitting.genre.get_descendants %}
{{ genre }},
{% endfor %}
</td>
Note: Also moved the td so you get the formatting style you wanted.