Django + Python, show modal using {% if %} in template

Asked

Viewed 997 times

3

I’m making a check if there are active classes, if there is should present a modal and not the registration form, but it is not working:

py views.:

from django.shortcuts import render, redirect
from django.core.mail import send_mail
from django.template.loader import get_template
from django.conf import settings
from .models import *
from .forms import *
from registrations.services import email, parentEmail

# CRIAR UM NOVO ALUNO
def create_student(request):
    form = StudentForm(request.POST or None)
    group = Group.objects.filter(active=True).first()
    show_modal = False

    if not group:
        show_modal = True

    if form.is_valid():
        group = Group.objects.filter(active=True).first()
        form.instance.group = group
        student = form.save()
        request.session['student_id'] = student.id
        return redirect('registrations:parent_student')

    return render(request, 'student-form-registration.html', {'form': form})

student-form-registrations.html:

<!-- Modal Fim Inscrição -->
{% if show_modal is True %}
    <div id="modal" class="modal fade" tabindex="-1" role="dialog" id="inscricaoEncerrada">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-12">
                            <div class="text-center bottom-border">
                                <img class="img-modal" src="{% static 'images/ilustra3.png' %}" />
                            </div>
                            <p class="text-modal text-center">Olá! As inscrições para a próxima turma da Estação Hack Teens estão encerradas. A próxima turma vai
                                acontecer nos dias 06 e 07 de Outubro, e as inscrições estarão abertas a partir do dia 03
                                de Setembro! Volte nessa data e não deixe de participar :) </p>
                            <div class="text-center">
                                <a data-dismiss="modal" class="btn btn-primary btn-estacao">Voltar para Estação Hack Teens</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endif %}

I don’t think you know how to use the {% if %} in the template, someone knows how to help me ?

2 answers

6


The code is ok - but you are not passing the variable show_modal from Python to template.

This has to be done on the line that has the template rendered (and of course, for each variable you want to use in the template):

return render(
      request, 
      'student-form-registration.html',
      {'form': form, 'show_modal': show_modal}
)

Besides, it doesn’t make much sense to use if ... is True - would work in this case, but not in many others and is uglier than saying "climb up" in Portuguese. The variable value is already "true" - so just use the variable itself as the expression of if:

 {%  if show_modal  %}   

No further comparison is needed - this hint is valid for both the template language, Python code and several other languages.

  • 1

    +1 by the pleonasm in the code, I will definitely use this comparison in the future. hahaha

  • 1

    Sorry there Dr. @jsbueno, but it was worth the explanation oksaoskaoskaoksaosk

  • In fact, the use of is True goes beyond the aesthetic problem in Python - since values that would normally be true in a if may not be exactly the same object as True (as the very number 1) .

  • It makes sense @jsbueno mt thanks for the explanation, now it’s already working out you are too!

3

Apparently all that remains is to expose the variable show_modal for the template. Just add to that show_modal at the render.

return render(request, 'student-form-registration.html', {
    'form': form,
    'show_modal': show_modal
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.