Display data with ListView with Django 1.8

1

I am using Django 1.8 with Python 3.4, I need to show the data of a table with ListView but it does not show me anything or show me any error.

Here is the code:

models.py

class Tipo_almacen(models.Model):
      descripcion = models.CharField(max_length=50)

      class Meta:
         db_table = 'Tipo_almacen'
         verbose_name_plural = "Tipo de Almacenes"
         verbose_name = "Tipo de Almacen"

      def __str__(self):
          return self.descripcion

views.py

from django.shortcuts import render
from mantenimiento.forms import Tipo_almacenForm
from django.http import HttpResponseRedirect
from django.views.generic import ListView
from appkardex.models import Tipo_almacen

def menu(request):
    return render(request, 'main.html')    

class ListaTipoAlmacen(ListView):
    model = Tipo_almacen
    template_name = 'cruds/lista_tipo_almacen.html'

    def get_context_data(self, **kwargs):
        context = super(ListaTipoAlmacen, self).get_context_data(**kwargs)
        lista_tipo_almacen = Tipo_almacen.objects.all()
        context['lista_tipo_almacen'] = lista_tipo_almacen
        return context

urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', views.menu),
    url(r'^$', ListaTipoAlmacen.as_view(), name='lista_tipo_almacen')
] + static(settings.STATIC_URL)

main.html

<a href="{% url 'lista_tipo_almacen' %}">Tipo almacen</a></li>

tipo_tipo_almacen.html

{% extends "main.html" %}

{% block contenido %}

   <h1>Tipo Almacen</h1>
   <ul>
      {% for lista in lista_tipo_almacen %}
        <li>{{ lista.descripcion }} </li>
      {% endfor %}
   </ul>

{% endblock %}

Waiting for your prompt help.

    
asked by Andres Vilca 21.01.2016 в 01:33
source

4 answers

2

ListView by default it sends a list of model objects that you pass to the model attribute and in the template you take it with the name of object_list you do not need to use the get_context_data method, I'll give you an example.

views.py

class ListaTipoAlmacen(ListView):
    model = Tipo_almacen
    template_name = 'cruds/lista_tipo_almacen.html'

lista_tipo_almacen.html

    {% extends "main.html" %}

    {% block contenido %}

     <h1>Tipo Almacen</h1>
     <ul>
        {% for element in object_list %}
          <li>{{ element.descripcion }} </li>
        {% endfor %}
     </ul>

    {% endblock %}

That's it, now if you want to change the name of the variable object_list , in the view use one more attribute: context_object_name = 'lista_de_objetos' and in the template replace object_list by lista_de_objetos

    
answered by 07.03.2016 в 00:36
1

Let's see, first, I think you're misusing Class-based Views, second, if I'm not wrong, your model is not called a list, it's called Tipo_almacen , so it would be for every Tipo_almacen in lista_tipo_almacen .

Try it that way, if it fails, you let me know.

    
answered by 21.01.2016 в 06:02
1

Mmm interesting,% default% co sends the object or list in context as ListView , I see that you use a function object_list you can use only get_context_data .

Example:

{% extends "main.html" %}

{% block contenido %}

    <h1>Tipo Almacen</h1>
    <ul>
    {% for lista in object_list %}
        <li>{{ lista.descripcion }} </li>
    {% empty %}
        <p>No hay una lista de almacenes</p>
    {% endfor %}
    </ul>

{% endblock contenido %}

views.py:

from appkardex.models import Tipo_almacen
'''
Si tu modelo esta a la par de esta view usa:
from .models import Tipo_almacen
'''

class ListaTipoAlmacen(ListView):
    model = Tipo_almacen
    template_name = 'cruds/lista_tipo_almacen.html'

urls.py:

Comment the URL, because it can give you problems by not closing with object_list :

urlpatterns = [
  url(r'^admin/', include(admin.site.urls)),
  #url(r'^', views.menu),
  url(r'^$', ListaTipoAlmacen.as_view(), name='lista_tipo_almacen')
] + static(settings.STATIC_URL)
    
answered by 26.01.2016 в 17:40
0

If I'm not mistaken, the problem is with your URLs, this line:

url(r'^', views.menu),

You are capturing everything because you have set ^ with nothing else, you should have completion using $ .

Your URLs could be according to what you have provided in your code:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.menu),
    url(r'^lista/$', ListaTipoAlmacen.as_view(), name='lista_tipo_almacen')
] + static(settings.STATIC_URL)

Update

On your form, you have to pass it to the context, it is not enough to import it:

class ListaTipoAlmacen(ListView):
    model = Tipo_almacen
    template_name = 'cruds/lista_tipo_almacen.html'

    def get_context_data(self, **kwargs):
        context = super(ListaTipoAlmacen, self).get_context_data(**kwargs)
        lista_tipo_almacen = Tipo_almacen.objects.all()
        form = Tipo_almacenForm()
        context['lista_tipo_almacen'] = lista_tipo_almacen
        context['form'] = form
        return context

And render it in your template:

{% extends "main.html" %}

{% block contenido %}

   <h1>Tipo Almacen</h1>
   <ul>
      {% for lista in lista_tipo_almacen %}
        <li>{{ lista.descripcion }} </li>
      {% endfor %}
   </ul>

   <form method="post">
       {% csrf_token %}
       {{ form.as_p }}
   </form>

{% endblock %}

I recommend you read:

answered by 21.01.2016 в 13:29