How to use back-end information on the front end with Python and Django?

Asked

Viewed 359 times

3

In my code Python I create an object called status as follows:

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'),
    }

In 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/register/status_list.html, where is the table 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>

I mean, in my code html with the table of the entire list of status, in order to access the id of status just let me know {{status.id}} within a loop. Now I need to access that same information in another file html, called index.html, for this I tried to do 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 by creating the loop inside the archive index.html I’m not trying to get any results.

I’m probably doing it incorrectly. In the table of status is working, but in another file not. All importare correct. What is the proper way for me to access my object status in another file html?

1 answer

0

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

For you get all the Status objects you will override the method get_context_data. 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 this you will have the status variable available in your template, in your case index.html. So you just loop the template

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

Browser other questions tagged

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