Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

301
Visualizações
How to get data from a django model and show it into a bootstrap modal popup?

i'm new in django framework, in my app i have two model, BoardsList and BoardLink:

class BoardsList(models.Model):

   master_code = models.CharField(max_length=6, primary_key=True)
   uc_code = models.CharField(max_length=20)
   description = models.CharField(max_length=200)
   customer_name = models.CharField(max_length=30)

   def __str__(self):
      return "MASTER_CODE = " + self.master_code

class BoardLink(models.Model):
   boardsList = models.ForeignKey(BoardsList, on_delete=models.CASCADE)
   configuration_sheet_link = models.CharField(max_length=300)
   wiring_diagram_link = models.CharField(max_length=300)

   def __str__(self):
      return self.boardsList_id

And the relative views are :

class BoardsListView(generic.ListView):
    template_name = 'index.html'
    context_object_name = 'boards_list'
    paginate_by = 75

    def get_queryset(self):
        query_filter = self.request.GET.get("find_master", None)
        if query_filter is not None:
           return BoardsList.objects.filter(Q(master_code__contains=query_filter) |
                                             Q(uc_code__contains=query_filter) |                                                 
                            Q(customer_name__contains=query_filter)).order_by('customer_name')

        return BoardsList.objects.order_by('master_code')
    
 class BoardLinkView(generic.DetailView):
    template_name = 'index.html'
    context_object_name = 'board_link_list'
    paginate_by = 1

    def get_queryset(self):
       query_filter = self.request.GET.get("find_master_link", None)
       if query_filter is not None:
           return BoardLink.objects.filter(Q(boardsList__exact=query_filter))

in my html page i correctly showed boardlist data into a table :

<tbody>
    {% for board in boards_list %}
       <tr>
        <td>{{ board.master_code }}</td>
        <td>{{ board.uc_code }}</td>
        <td>{{ board.description }}</td>
        <td>{{ board.customer_name }}</td>
        <td>
            <form method="GET" action="">
            <button type="button" class="btn"  name="find_master_link" data-bs-toggle="modal" data-bs-target="#boardLinksModal">
                <i class="fa-solid fa-file-lines"></i>
            </button>
            </form>
        </td>
       </tr>
    {% endfor %}
  </tbody>

now my problem is how i can get BoardLink model data record on button click event and show it on bootstrap modal popup that i have already created and include it on my html page with :

 {% include 'modalPopUp.html' %}

my modal code is the same of bootstrap documentantions example.

thanks in advance for the answers!!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is really a question on how templates work. What you have to understand is that when a client requests a page to a django server, the django view renders the template before sending it back to the browser. This means that it processes all the dynamic tags in the template and converts it to a static html page, which it sends to the browser requesting it.

This includes the {% include 'page.html'%} tag, which by the way, is super great.

When a template is rendered, if there are any {% include 'page.html'%} tags inside, what happens is that the html files being referenced are fetched, rendered to static html, and finally embedded in the root template.

All of it is sent a a single static html page to the client browser. This means that while you may break up your template in several files in order to keep it tidy, when the page is sent to a client, it is in the form of a unique fully static html file, that was built resolving all the dynamic components in the form of django template tags.

I hope to have been clear up to now..

If you understood my explanation, you will also understand that every sub-template being included in the root template also has access to the same context variables as the root template itself.

So in your case, if you include a bit of html that contains a bootstrap modal, you can reference the same context variables available to you in the page that contains the include tag. So go ahead, try adding {{ boards_list }} or {{ board_link_list }} in your modal body and you'll see that it works right out of the box.

P.S. BoardLinkView is a DetailView and your context_object_name is board_link_list which is very misleading because it is not a list. I would change it if I were you!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda