Home > Software design >  My if statement is not working, I am using django and python and I know the if statement is true
My if statement is not working, I am using django and python and I know the if statement is true

Time:12-14

I am working with django and python trying to create an application, for some reason the following if statement: {% if zone.place_of_table == x.table_place_preference %} is not working, but I know it should work because the if statement is true as you can see in the output image I attached.

Here are my models

Reservation model:

# Create your models here.
class reservations(models.Model):
    shifts = (
        ('Breakfast', 'Breakfast'),
        ('Lunch', 'Lunch'),
        ('Dinner', 'Dinner'),
    )
    restaurant = models.ForeignKey(restaurants,related_name='restaurant_of_reservation',on_delete=models.CASCADE)
    full_name = models.CharField(max_length=254)
    email = models.EmailField(max_length=254)
    phone_number = PhoneNumberField()
    date = models.DateField()
    shift = models.CharField(max_length=254,choices=shifts,default=shifts[0][0])
    time = models.CharField(max_length=254)
    estimatedtime = models.CharField(max_length=254,default='')
    tablesused = models.CharField(max_length=254,default='')
    table_place_preference = 
models.ForeignKey(placeOfTable,related_name='placeoftable',on_delete=models.CASCADE)
    number_of_people = models.PositiveIntegerField()
    baby_chair = models.PositiveIntegerField()
    message = models.CharField(max_length=254)

    def __str__(self):
        return self.full_name

zone model:

class placeOfTable(models.Model):
    restaurant = models.ForeignKey(restaurants,related_name='restaurantplaceoftable',on_delete=models.CASCADE)
    place_of_table = models.CharField(max_length=254,unique=True)

    def __str__(self):
        return self.place_of_table

views:

def restaurantmenu(request, restaurant_pk):
    zones = placeOfTable.objects.filter(restaurant__pk__contains=restaurant_pk)
    today = date.today()
    date_true = 'False'
    choosenday = today.strftime("%Y-%m-%d")
    owner_restaurants = restaurants.objects.filter(owner__username__contains=request.user)
    restaurant = restaurants.objects.filter(owner__username__contains=request.user,pk__contains=restaurant_pk)
    reservation_breakfast = reservations.objects.filter(restaurant__pk__contains=restaurant_pk,
                                                        date__contains=choosenday,
                                                        shift__contains='Breakfast')
    reservation_lunch = reservations.objects.filter(restaurant__pk__contains=restaurant_pk,
                                                    date__contains=choosenday,
                                                    shift__contains='Lunch')
    reservation_dinner = reservations.objects.filter(restaurant__pk__contains=restaurant_pk,
                                                     date__contains=choosenday,
                                                     shift__contains='Dinner')
    choosenday = datetime.datetime.strptime(choosenday,"%Y-%m-%d")
    choosenday = choosenday.strftime("%m/%d/%Y")
    if request.method == 'POST':
        choosenday = request.POST.get('choosenday',today.strftime("%Y-%m-%d"))
        choosenday = datetime.datetime.strptime(choosenday,"%m/%d/%Y")
        reservation_breakfast = reservations.objects.filter(restaurant__pk__contains=restaurant_pk,
                                                            date__contains=choosenday.strftime("%Y-%m-%d"),
                                                            shift__contains='Breakfast')
        print(reservation_breakfast[0])
        reservation_lunch = reservations.objects.filter(restaurant__pk__contains=restaurant_pk,
                                                        date__contains=choosenday.strftime("%Y-%m-%d"),
                                                        shift__contains='Lunch')
        reservation_dinner = reservations.objects.filter(restaurant__pk__contains=restaurant_pk,
                                                         date__contains=choosenday.strftime("%Y-%m-%d"),
                                                         shift__contains='Dinner')
        choosenday = choosenday.strftime("%m/%d/%Y")

    return render(request, 'restaurants/restaurantmenu.html',{'restaurant':restaurant,
                                                              'owner_restaurants':owner_restaurants,
                                                              'reservation_breakfast':reservation_breakfast,
                                                              'reservation_lunch':reservation_lunch,
                                                              'reservation_dinner':reservation_dinner,
                                                              'choosenday':choosenday,'zones':zones})

html template:

          {% if reservation_breakfast %}
          {% for zone in zones %}
          <table >
            <thead >
              <tr>
                <td></td>
                <td></td>
                <td>{{zone.place_of_table}}</td>
                <td></td>
                <td></td>
              </tr>
            </thead>
            <tbody >
              {% for x in reservation_breakfast %}
              <p>{{x.table_place_preference}} == {{zone.place_of_table}} ?</p>

                {% if zone.place_of_table == x.table_place_preference %}
                  <tr>
                    <td>Mark</td>
                    <td>Otto</td>
                    <td>@mdo</td>
                  </tr>
                {% endif %}
              {% endfor %}
            </tbody>
          </table>
          {% endfor %}
          {% endif %}

The output is the following: It should print the table line with the words Mark Otto and @mdo right? So does anyone can tell me what am I doing wrong?

CodePudding user response:

Try this: May be one as string and other as boolean using safe tag we can make as same

{% if zone.place_of_table|safe == x.table_place_preference|safe %}
 <tr>
 <td>Mark</td>
 <td>Otto</td>
 <td>@mdo</td>
 </tr>
{% endif %}
  • Related