I struggled with conditional branching in Django's Templates.

Thing you want to do

I'm trying to make it possible to reserve equipment for a certain circle from the Web with Django To prevent you from editing or deleting past reservation data I want to hide the edit button and delete button of an appointment before the current time.

I had a hard time trying to describe the conditional branch for that in templates.

views.py


context = {
    'band_name' : band_name,
    'schedules' : Schedule.objects.all(),
    'target_date' : datetime.datetime.now()
}
return render(request, 'xxx.html', context)

xxx.html


{% schedule in schedules %}
  {% if schedule.target_date > target_date %}
Button display
  {% endif %}
{% endfor %}

Pass the variable (today's date) and model to templates I want to control the display / non-display of buttons by conditional branching by date in the schedules loop.

However, the conditional branch cannot be done well. Simply {% if True %} Then, the conditional branch was made well, so judge in advance with view, and use the model id and judgment result (Bool) as a dictionary. I passed it to templates and tried to control it

views.py


my_dic = {}
target_date = datetime.datetime.now()
for schedule in schedules:
  if target_date < schedule.target_date:
    my_dic[schedule.id] = True
  else:
    my_dic[schedule.id] = False 
context = {
    'schedules' : Schedule.objects.all(),
    'my_dic' : my_dic,
}
return render(request, 'xxx.html', context)

xxx.html


{% schedule in schedules %}
  {% if my_dic[schedule.target_date] %}
Button display
  {% endif %}
{% endfor %}

But this also doesn't work.

Expanding the dictionary in templates my_dic.id You can expand by specifying the key for id in, but I want to pass the schedule.id in the schedules loop to this ".id" part. This also doesn't work. Assigning a variable using the with tag doesn't work.

{% with target_id = schedule.id %} {% if my_dic[schedule.target_id] %}

This also doesn't work.

{% if my_dic.schedule.id %} {% if my_dic[schedule.id] %} These were of course no good.

solution

I made a method directly on the model.

models.py


class Schedule(models.Model):
  #Column definition ...
  target_date = models.DateField(default=timezone.now, verbose_name=‘Reservation date’)

  def is_available(self):
    current_day = timezone.datetime.today()
    return current_day < self.active_date

xxx.html


{% schedule in schedules %}
  {% if schedule.is_available %}
Button display
  {% endif %}
{% endfor %}

Done.

If you use this method, you can redirect even direct access by URL.

Isn't the coding that I wrote about what I want to do not working? Of course, it is better not to write logic in templates as much as possible, I wanted small operations such as looking up a dictionary to work well.

I found other solutions such as creating templates filter by myself, I think this method is the most beautiful for now.

that's all.

P.S. Mr. Chanyu (@ chanyou0311) taught me. Thank you.

Recommended Posts

I struggled with conditional branching in Django's Templates.
I tried to integrate with Keras in TFv1.1
I want to start over with Django's Migrate
What should I do with DICOM in MPEG2?
I want to transition with a button in flask
I was in trouble because I couldn't push with heroku
I was addicted to scraping with Selenium (+ Python) in 2020
I want to work with a robot in python.
Storage I / O notes in Python with Azure Functions
I can't manipulate iframes in a page with Selenium
Sort post data in reverse order with Django's ListView
Note that I dealt with HTML in Beautiful Soup
Work memo that I tried i18n with Flask app
Clearly write code that increases conditional branching in multiplication