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

2

I'm doing a check if there are active classes, if there is not a modal and not the registration form, but it is not working:

views.py:

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 do not know how to use {% if %} in the template, does anyone know how to help me?

    
asked by anonymous 17.09.2018 / 15:03

2 answers

5

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

This has to be done on the line that renders the template (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}
)

In addition, it does not make much sense to use if ... is True - it would work in this case, but not in many others and it's uglier to say "climb up" in Portuguese. The value of the variable is already "true" - so just use the variable itself as the expression of if :

 {%  if show_modal  %}   

You do not need any other comparison - this tip is valid for both the template language, Python code and several other languages.

    
17.09.2018 / 15:10
3

Apparently all you need to do is expose the variable show_modal to the template. To do this, just add show_modal to render .

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