html – How to use backend information in frontend with Python and Django?

Question:

In my Python code I create an object called status like this:

In forms/status.py

class StatusForm(forms.ModelForm):

    class Meta:
        model = Status
        fields = ('status', 'posicao')
        widget = {
            'status': forms.TextInput(attrs={'class': 'form-control'}),
            'posicao': forms.TextInput(attrs={'class': 'form-control'}),
    }
        labels = {
            'status': _('Descrição'),
            'posicao': _('Posição da coluna na navegação'),
    }

Em models/status.py

class Status(models.Model):
    status = models.CharField(max_length=32)
    posicao = models.IntegerField(default=1)

    class Meta:
        verbose_name = "Status"
        permissions = (
            ("view_status", "Can view status"),
        )

And the following code in views/status.py

class AdicionarStatusView(AdicionarOutrosBaseView):
    form_class = StatusForm
    model = Status
    success_url = reverse_lazy('cadastro:addstatusview')
    permission_codename = 'add_status'


 class StatusView(CustomListView):
    model = Status
    template_name = 'cadastro/status_list.html'
    context_object_name = 'all_status'
    success_url = reverse_lazy('cadastro:listastatusview')
    permission_codename = 'view_status'

In templates/cadastro/status_list.html, where the table is for me to see the status that have already been created, I do the following:

<div class="table-responsive">
      <table id="lista-database" >
            <thead>
               <tr>
                   <th>#ID</th>
                    <th>Descrição</th>
                    <th>Posição na Navegação</th>
                </tr>
              </thead>
              <tbody>   
                    {% for status in all_status %}
                        <td>{{status.id}}</td>
                        <td>{{status.status}}</td>
                        <td>{{status.posicao_navegacao}}</td>
                    {% endfor %}
               </tbody>
      </table>
</div>

In other words, in my html code with the table of the entire status list, in order to access the status id I just enter {{status.id}} inside a loop . Now I need to access this same information in another html file, called index.html , so I tried to do it this way inside views/status.py :

class StatusListEmNavegacaoView(CustomListView):
    model = StatusVenda
    template_name = 'crm/navegacao/index.html'
    context_object_name = 'all_status'
    success_url = reverse_lazy('crm:navegacaoview')
    permission_codename = 'view_status'

But when creating the loop inside the index.html file I'm not getting any results.

I'm probably doing it incorrectly. It is working in the status table, but not in another file. All import are correct. What is the proper way for me to access my status object in another html file?

Answer:

Your StatusListEmNavegacaoView class is importing the StatusVenda model and not the Status model. In other words, the object available in your template will be the StatusSales and not the Status. From what I understand you need to have two objects in your template.

To get all Status objects you will override the get_context_data method. With it you create objects that are available in your template.

from .models import Status

class StatusListEmNavegacaoView(CustomListView):
   model = StatusVenda
   template_name = 'crm/navegacao/index.html'
   context_object_name = 'all_status'
   success_url = reverse_lazy('crm:navegacaoview')
   permission_codename = 'view_status'

   def get_context_data(self, **kwargs):
      context = super().get_context_data(**kwargs)
      context['status'] = Status.objects.all()
      return context

With that you will have the status variable available in your template, in your case index.html . So you just loop in the template

{% for s in status %}
   <td>{{s.id}}</td>
   <td>{{s.status}}</td>
   <td>{{s.posicao}}</td>
{% endfor %}
Scroll to Top