Setting specific day to print specific message in the django template - django

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.

Related

Cant write correct queryset in 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 %}

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

Django - How to change dropdown form into table

I'm creating an application in Django and I'm having an issue with iterating through options in a dropdown from a form in Django. I was wondering if anyone could be of assistance.
detail.html
{% for subject in subject_list %}
<form action="" method="POST">
{% csrf_token %}
<table class='table table-borderless table-hover'>
<tbody>
<tr>
<td>{{ subject.subjectname }}
{% for field in form %}
{{ field.hidden }}
{% endfor %}
</td>
</tr>
</tbody>
</table>
<button type="submit" id="login-btn" class="btn">Create a new evaluation</button>
</form>
{% endfor %}
forms.py
class CreateEvalForm(forms.ModelForm):
subjectname = ModelChoiceField(queryset=Subject.objects, label='', empty_label="Choose subject..")
class Meta:
model = Evaluation
fields = ['subjectname', ]
models.py
class Subject(models.Model):
id = models.AutoField(primary_key=True)
subjectname = models.CharField(max_length=255, help_text="Type the name of the subject.")
slug = models.SlugField(max_length=200, unique=True)
Ideally we want it to look like on the picture below:
Note that the picture is in danish and Fag = Subject, Opret ny evaluering = Create new evaluation and Se evaluering = View evaluation
we want this
we don't want this
I solved this with the help from the comments by changing my for loop and table to this:
<form action="" method="POST">
{% csrf_token %}
<table class='table table-borderless table-hover'>
{% for option in form.fields.fagnavn.choices %}
{% if not forloop.first %}
<tbody>
<tr>
<td>{{ option.1 }}</td>
<td><button value="{{ option.0 }}" name="fagnavn">Opret ny evaluering</button></td>
</tr>
</tbody>
{% endif %}
{% endfor %}
</table>
</form>
I gained access to the choices in the dropdown with form.fields.fagnavn.choices and excluded the default value of "choose value" with an if statement of {% if not forloop.first %}
And that's pretty much it.

Displaying data from intermediate table in a many-to-many relation

I have 3 models that follow the principle described in Django documentation (https://docs.djangoproject.com/en/2.2/topics/db/models/#extra-fields-on-many-to-many-relationships):
class Topic(models.Model):
key = models.CharField(max_length=255, primary_key=True)
persons = models.ManyToManyField('Person', through='Interest')
class Person(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
interests = models.ManyToManyField('Topic', through='Interest')
class Interest(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
source = models.ForeignKey(TopicSource, on_delete=models.CASCADE)
The views.pyis really simple:
class TopicsView(generic.ListView):
template_name = 'app/topics.html'
context_object_name = 'topics_list'
def get_queryset(self):
return Topic.objects.all()
The template is actually giving me headaches:
<table>
<tbody class="list">
{% for item in topics_list %}
<tr>
<td>{{ item.key }}</td>
<td>{{ item.person_set.all.count }}</td>
<td>
<ul>
{% for person in item.person_set.all %}
<li>{{ person.last_name }}, {{ person.first_name }} [{% person.interests_set.get(cluster=item) %}]</li>{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
With {% person.interests_set.get(topic=item) %}, I'm trying to access data from the intermediate table.
How can I display the source of the interest next to the name of the person?
This solution is giving hint on how to do this from the shell but I cannot figure out how to achieve that in the template.
Django templates don't support what you're trying to do out of the box. You would have to create a custom tag to implement what you're trying.
However you could do:
class TopicsView(generic.ListView):
template_name = 'app/topics.html'
context_object_name = 'topics_list'
queryset = Topic.objects.prefetch_related(
Prefetch(
'interest_set',
Interest.objects.select_related('person')
)
Then adjust your template to iterate over the interests.
<table>
<tbody class="list">
{% for item in topics_list %}
<tr>
<td>{{ item.key }}</td>
<td>{{ item.interest_set.all.count }}</td>
<td>
<ul>
{% for interest in item.interest_set.all %}
<li>{{ interest.person.last_name }}, {{ interest.person.first_name }} [ {{ interest.source }} ]</li>{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
As an alternative to the solution proposed by #schillingt (the accepted solution), I came up with the below solution:
views.py:
class InterestsView(generic.ListView):
template_name = 'app/interests.html'
context_object_name = 'interests_list'
def get_queryset(self):
return Interest.objects.order_by('topic', 'person').all()
template:
{% regroup interests_list by topic as l1%}
<table>
<tbody>
{% for topic in l1 %}
<tr>
<td>{{ topic.grouper }}</td>
<td>{{ topic.list|length }}</td>
<td>
<ul>
{% for item in topic.list%}
<li>{{ item.person.last_name }}, {{ item.person.first_name }} [{{ item.source }}]</li>
{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
However, the solution from #schillingt is better since it's performance are a lot better (by a factor 2),

Django: How to add a new button in admin/index.html for specific model

I am overriding the admin/index.html content block.
I have this inside:
{% for model in app.models %}
<tr class="model-{{ model.object_name|lower }}">
{% if model.admin_url %}
<th scope="row">{{ model.name }}</th>
{% else %}
<th scope="row">{{ model.name }}</th>
{% endif %}
{% if model.add_url %}
<td>{% trans 'Add' %}</td>
{% else %}
<td> </td>
{% endif %}
{% if model.admin_url %}
<td>{% trans 'Change' %}</td>
{% else %}
<td> </td>
{% endif %}
</tr>
{% endfor %}
and my model is:
class Schedule(models.Model):
active = models.BooleanField(default=True, null=False)
price = models.DecimalField(max_digits=10, decimal_places=2, null=False)
price_percent_lower = models.DecimalField(max_digits=2, decimal_places=1, null=False, default=1.3)
price_percent_highter = models.DecimalField(max_digits=2, decimal_places=1, null=False)
How can I create a new button like {% trans 'Add' %} pointing to a method named: do_a_thing() only for this model?
If you have to just call on template the do_a_thing this should work.
{% if model.object_name == 'Schedule' %}
{% trans 'Do a thing' %}
{% endif %}
And in model
class Schedule(models.Model):
active = models.BooleanField(default=True, null=False)
price = models.DecimalField(max_digits=10, decimal_places=2, null=False)
price_percent_lower = models.DecimalField(max_digits=2, decimal_places=1, null=False, default=1.3)
price_percent_highter = models.DecimalField(max_digits=2, decimal_places=1, null=False)
#classmethod
def do_a_thing(cls):
...
If you need add views here is a good post about that http://patrick.arminio.info/additional-admin-views/