Adnan Sheikh
Adnan Sheikh

Reputation: 806

If condition is not working in Inner Loop in django templates

I don't know what is the problem and I've been stuck here for hours. There maybe duplicate questions but none of them could get me out of this.

I am using an if condition in the inner loop to check if the attribute of inner is equal to the outer loop but if condition is never true even the data is same.

I've printed the data individually, both attributes print the data mean data is correct. But when I use if condition it goes to else

Data

Here's the data I'm working with:

activityy.activity_name = [Table Tennis,Swimming Pool, Football ] 
slot.activity = [Table Tennis,Table Tennis,Table Tennis,Table Tennis,Swimming Pool, Football]

activities.html

{% for activityy in all_activities%}
    <div style="margin-left: 450px; padding: 15px; border-radius: 5px; background-color: #dfdfdf;height: 150px; width: 800px; margin-top: 20px; text-align: center">
    {% for slot in all_slots  %}
        {% if slot.activity == activityy.activity_name %}
            <div style="background-color: #3a589a; padding: 15px; width: 120px; height: 120px; float: left; border-radius: 5px;">
                <span style="color: #f1f1f1; font-size: 20px;"> {{ activityy.activity_name}}</span><br>
            </div>
        {% else %}
            <div style="background-color: #589a; padding: 15px; width: 120px; height: 120px; float: left; border-radius: 5px;">
                <span style="color: #f1f1f1; font-size: 20px;"> {{ slot.activity}}</span><br>
            </div>
        {% endif %}
    {% endfor %}
    </div>
{% endfor %}

Views.py

def activities(request):
    if request.user.is_authenticated:
        template = loader.get_template('sklc/activities.html')
        slots = []
        now = datetime.datetime.now()
        datee = now.strftime("%Y-%m-%d")
        if request.method == 'POST':
            dat = request.POST['date']
            if dat:
                datee = dat

        print("dateesss: " , datee)
        activitiess = Activities.objects.all();
        for activityy in activitiess:
            slot = ActivitySlots.objects.filter(dates=datee).filter(activity__activity_name=activityy.activity_name)
            for slott in slot:
                slots.append(slott)

        context = {
            'all_activities': activitiess,
            'all_slots': slots
        }
        return HttpResponse(template.render(context, request))
    else:
        messages.error(request, "Please Login First")
        return redirect("/login")

models.py

class Activities(models.Model):
    activity_name = models.CharField(max_length=50)

    def __str__(self):
        return self.activity_name

class ActivitySlots(models.Model):
    dates = models.DateField()
    available = models.BooleanField()
    activity = models.ForeignKey(Activities)
    time = models.CharField(max_length=50)

    def __str__(self):
        return self.time

Upvotes: 1

Views: 78

Answers (1)

Adnan Sheikh
Adnan Sheikh

Reputation: 806

I solved it by using

{% if slot.activity == activityy %}

Butt still don't know why it was not working with activityy.activity_name because activityy and activityy_activity_name are printing the same thing.

Upvotes: 2

Related Questions