Silreth
Silreth

Reputation: 85

Django html IF statement not comparing model variables

Trying to nest an IF statement in FOR loops to segment videos by a parent variable. However, my if statement doesn't seem to be recognising matches.

I can confirm that both output some matching numbers in their respective statements individually. Not sure what I'm missing here, any help would be appreciated.

Profile.html

{% for x in source %}
    <div class="content-section">
      <div class="media-body">
      <h2 class="account-heading">{{ x.video_id }}</h2>
      {% for y in records %}
        <h1 class="text-secondary">{{ y.sourcevideo.video_id }}</h1>
        {% if '{{ x.video_id }}' == '{{ y.sourcevideo.video_id }}' %}
          <video width="320" height="240" controls>
            <source src="{{ y.highlight }}" type="video/mp4">
              Your browser does not support the video tag
            </video>
        {% else %}
        <h1 class="text-secondary">No Matches</h1>
        {% endif %}
      {% endfor %}
      </div>
    </div>
  {% endfor %}

views.py

class ProfileView(ListView):
    model = Highlight
    template_name = 'users/profile.html'
    context_object_name = 'records'
    ordering = ['-date_created']

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def get_queryset(self):
        return Highlight.objects.filter(creator=self.request.user)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['source'] = Video.objects.filter(creator=self.request.user)
        return context

Upvotes: 0

Views: 134

Answers (1)

dirkgroten
dirkgroten

Reputation: 20692

Your statement is comparing two different strings: "{{ x.video_id }}" and "{{ y.videosource.video_id }}".

You need to compare the variables: {% if x.video_id == y.sourcevideo.video_id %}.

Upvotes: 1

Related Questions