-1

I don't know to how configure views.py and urls.py in myapp. for filter a list between dates from an input date in a template.

This is my models.py:

class Paciente(models.Model):
   tipo_doc = models.ForeignKey(Tipo_doc)
   num_doc = models.CharField(max_length=20, primary_key=True)
   ...

class Consulta (models.Model):
   numero = models.ForeignKey(Paciente)
   fecha = models.DateField()
   ...      

This is my file "lista.html" in templates:

<h5>
  Fecha desde: <input class="inputDate" id="fechadesde" value={{ fecha_d }} />
  Fecha hasta: <input class="inputDate" id="fechahasta" value={{ fecha_h }} />
  <a href="/clinica/filtrar_consulta" class="button">Filtrar</a>
</h5>

<ul class="actions">
 ...             

I need to filter the list by paciente and between 2 dates (fecha_d, fecha_h), but I don't know how to pass parameters in url. Thank You

Fabio
  • 9
  • 3
  • 1) https://docs.djangoproject.com/en/dev/topics/forms/ 2) https://docs.djangoproject.com/en/dev/ref/models/querysets/#range – madzohan Dec 15 '14 at 19:33

1 Answers1

0

First of all, I'd use a form to do the filtering:

lista.html:

<form method='get' action='.'>
    Fecha desde: <input class="inputDate" name='fechadesde' />
    Fecha hasta: <input class="inputDate" name="fechahasta" />
    <input type='submit' value='Filtrar'/>
</form>

Then, in your view, you need to retrieve these values and filter the queryset based on that. This is a code fragment that could be used either in a function or class based view:

qs = Paciente.objects.all()
fecha_desde = request.GET.get('fechadesde', None)
fecha_hasta = request.GET.get('fechahasta', None)
if fecha_desde:
    qs = qs.filter(fecha__gte=fecha_desde)
if fecha_hasta:
    qs = qs.filter(fecha__lte=fecha_hasta)
context = {'consultas': qs}

And go from there. A good enhancement would be to use django form system instead of manually writting your html and performing manual validation

Read more about them here

Alvaro
  • 11,797
  • 9
  • 40
  • 57
  • Hi, I did what you say, but "request.GET.get" return value always is None – Fabio Dec 19 '14 at 19:26
  • Are you sure you added the "name" attribute to the inputs? – Alvaro Dec 19 '14 at 19:29
  • this is my html:          Filtrar – Fabio Dec 19 '14 at 19:43
  • Alvaro, sos de Rosario y yo tratando de escribir en INGLES JAJA – Fabio Dec 19 '14 at 19:45
  • jajaja terrible...! fijate que el name dice "fecha_desde",entonces en request.GET.get() usa el mismo nombre 'fecha_desde' – Alvaro Dec 19 '14 at 19:52
  • Si ahi no te sale, revisa el valor que tiene request.GET.. pero deberia funcionar bien si el form tiene method="get" – Alvaro Dec 19 '14 at 19:53
  • este es el views.py: ` ....fecha_desde = request.GET.get('fecha_desde') fecha_hasta = request.GET.get('fecha_hasta') numero1 = request.GET.get('num_paciente') consultas = Consulta.objects.all() consultas = consultas.filter(numero=numero1) data['object_list'] = consultas return render(request, template_name,data)` – Fabio Dec 19 '14 at 20:00
  • Quiero que aunque sea me filtre por num_paciente(la variable se llama numero1) pero tampoco anda, lo de las fechas lo veo despues – Fabio Dec 19 '14 at 20:03
  • Mandame el archivo de views y el html al mail si tenes un segundo, asi lo veo bien... alvarotuso@hotmail.com – Alvaro Dec 19 '14 at 20:31