Using filter in Django

I have my function buscar, but I get this error:

enter the description of the image here

Here I leave the Code:

View.py

class ListarTipoAlmacen(ListView):
    model = Tipo_almacen
    template_name = 'tipo_almacen/lista_tipo_almacen.html'
    paginate_by = 10

    def get(self, request, *args, **kwargs):
        buscar_descripcion = request.GET.get('buscar_descripcion', '')

        lista_tipo_almacen = Tipo_almacen.objects.filter(descripcion__contains=buscar_descripcion)

        if buscar_descripcion == '':
            context['error'] = 'ingrese dato por favor'
        else:
            if not buscar_descripcion:
                context['error'] = 'el dato ingreso no existe'

        return render_to_response('tipo_almacen/lista_tipo_almacen.html', {'lista_tipo_almacen': lista_tipo_almacen}, context['error'])


    def get_context_data(self, **kwargs):
        context = super(ListarTipoAlmacen, self).get_context_data(**kwargs)
        lista_tipo_almacen = Tipo_almacen.objects.all().order_by('descripcion')

        paginator = Paginator(lista_tipo_almacen, self.paginate_by)
        page = self.request.GET.get('page')

        try:
            pagina = paginator.page(page)
        except PageNotAnInteger:
            pagina = paginator.page(1)
        except EmptyPage:
            pagina = paginator.page(paginator.num_pages)

        context['lista_tipo_almacen'] = pagina
        return context

HTML

<div class="row">
        <div class="col-lg-12">
            <div class="form-group">
              <div class="col-md-4">
                <input id="textinput" name="textinput" placeholder="ingrese nombre" class="form-control input-md" type="text">
              </div>
            </div>

            <div class="form-group">
                <div class="col-md-2">
                    <button id="singlebutton" name="singlebutton" class="btn btn-primary">Buscar</button>
                </div>
            </div>
        </div>
    </div>
    <br><br>


    <div class="panel panel-primary">
        <div class="panel-heading">
            <h4>Lista de Tipo de almacen</h4>
        </div>

        <table class="table">
            <th>Nombres</th>
            <th>Acciones</th>
            <tbody>
                {% for data in lista_tipo_almacen %}
                    <tr>
                        <td>
                            {{data.descripcion}}
                        <td>

                            <a href="{% url 'editar_tipo_almacen' data.pk %}">Editar <span class="glyphicon glyphicon-edit"></span></a>
                            <a href="{% url 'eliminar-tipo-almacen' data.pk %}" data-toggle="modal_almacen" data-target="#modal_almacen" data-id="{{ data.descripcion }}">Eliminar <span class="glyphicon glyphicon-trash"></span></a>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
 0
Author: FJSevilla, 2016-03-23

2 answers

What you need is to use the get method and there filter the information and then pass it to the template.

An example:

def get(self, request, *args, **kwargs):
    name_search = request.GET.get('name_search', '')
    if not name:
        raise Http404

    lista_tipo_almacen = Tipo_almacen.objects.filter(
        descripcion__contains=name_search,
    )
    return render_to_response(
        'store_search.html',
        {
            'lista_tipo_almacen': lista_tipo_almacen,
        }
    )
 1
Author: Felipe Zuluaga, 2016-03-30 18:39:36

If you want to include the pagination try this:

View.py

class ListarTipoAlmacen(ListView):
    model = Tipo_almacen
    template_name = 'tipo_almacen/lista_tipo_almacen.html'

def get(self, request, *args, **kwargs):
    errores = []
    if request.method == 'GET':
        buscar_descripcion = request.GET.get('buscar_descripcion', '')
        if not request.GET.get('buscar_descripcion', ''):
            errores.append('Ingrese dato por favor')
        else:
            lista_tipo_almacen = Tipo_almacen.objects.filter(descripcion__icontains=buscar_descripcion)

            paginator = Paginator(lista_tipo_almacen, 10)

            parametros = request.GET.copy()
            if parametros.has_key('pagina'):
                del parametros['pagina']

            page = request.GET.get('pagina')
            try:
                queryset = paginator.page(page)
            except PageNotAnInteger:

                queryset = paginator.page(1)
            except EmptyPage:

                queryset = paginator.page(paginator.num_pages)

            context = {
                "objetc_list": queryset,
                "parametros": parametros,
            }
            return render(request, 'tipo_almacen/lista_tipo_almacen.html', context)
    return render(request, 'tipo_almacen/lista_tipo_almacen.html', {'errores': errores})

HTML

<div class="row">
      {% if errores %}

          {% for error in errores %}
            <h5>{{ error }}</h5>
          {% endfor %}

      {% endif %}

    <div class="col-lg-12">
        <div class="form-group">
          <div class="col-md-4">
            <input id="textinput" name="textinput" placeholder="ingrese nombre" class="form-control input-md" type="text">
          </div>
        </div>

        <div class="form-group">
            <div class="col-md-2">
                <button id="singlebutton" name="singlebutton" class="btn btn-primary">Buscar</button>
            </div>
        </div>
    </div>
</div>
<br><br>

 {% if objetc_list %}
<div class="panel panel-primary">
    <div class="panel-heading">
        <h4>Lista de Tipo de almacen</h4>
    </div>

    <table class="table">
        <th>Nombres</th>
        <th>Editar</th>
        <th>Borrar</th>
        <tbody>
            {% for obj in objetc_list %}
                <tr>
                    <td>{{obj.descripcion}}</td>
                    <td><a href="{% url 'editar_tipo_almacen' obj.pk %}">Editar <span class="glyphicon glyphicon-edit"></span></a></td>
                    <td><a href="{% url 'eliminar-tipo-almacen' obj.pk %}" data-toggle="modal_almacen" data-target="#modal_almacen" data-id="{{ obj.descripcion }}">Eliminar <span class="glyphicon glyphicon-trash"></span></a></td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

  <ul class="pagination">
    {% if objetc_list.has_previous %}
      <li class="waves-effect"><a href="?pagina={{ objetc_list.previous_page_number }}{% if parametros.urlencode %}&{{ parametros.urlencode }}{% endif %}"><i class="material-icons">chevron_left</i></a></li>
      {% else %}
      <li class="disabled"><a href="#!"><i class="material-icons">chevron_left</i></a></li>
    {% endif %}

    {% for num in objetc_list.paginator.page_range %}
      {% ifequal num objetc_list.number %}
        <li class="active"><a href="#!">{{ num }}</a></li>
      {% else %}
        <li class="waves-effect"><a href="?pagina={{ num }}{% if parametros.urlencode %}&{{ parametros.urlencode }}{% endif %}">{{ num }}</a></li>
      {% endifequal %}
    {% endfor %}

    {% if objetc_list.has_next %}
      <li class="waves-effect"><a href="?pagina={{ objetc_list.next_page_number }}{% if parametros.urlencode %}&{{ parametros.urlencode }}{% endif %}"><i class="material-icons">chevron_right</i></a></li>
      {% else %}
      <li class="disabled"><a href="#!"><i class="material-icons">chevron_right</i></a></li>
    {% endif %}
  </ul> 

{% else %} 

<p>El dato ingresado no existe</p>  

{% endif %} 

</div>
 0
Author: Eibi76, 2016-03-30 18:13:06