HOW TO Configure url and view for filter between two dates

55 views
Skip to first unread message

Efe

unread,
Dec 15, 2014, 5:36:08 PM12/15/14
to django...@googlegroups.com
Hello,  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_dfecha_h), but I don't know how to pass parameters in url. Thank You

Héctor Urbina

unread,
Dec 15, 2014, 7:11:55 PM12/15/14
to django...@googlegroups.com
Hello,

In urls.py you define the url that serves the page you're building, something like this would work:

from django.conf.urls import patterns, url

from <myapp> import views

urlpatterns
= patterns('<myapp>.views',
    url
(r'^$',views.index, name="index"),
    url
(r'^listarConsultas/$', views.listarConsultas, name="listarConsultas"),
)

in views.py you create that "views.listarConsultas" declared in the last line of urls.py:

def listarConsultas(request):
   
if request.method == "POST":
        fecha_desde
= request.POST["fechadesde"]
        fecha_hasta
= request.POST["fechahasta"]
       
...

In order to let the user choose the paciente, I recommend you to use a ModelForm

good luck,
Hector.
Message has been deleted

Héctor Urbina

unread,
Dec 15, 2014, 7:33:52 PM12/15/14
to django...@googlegroups.com
I'm sorry, I missleaded you,

what you really want to use is a ModelChoiceField in a custom form. So you can create a form with the following code:

from django import forms

class pacienteConsultasForm(forms.Form):
 fecha_desde
= forms.DateField()
 fecha_hasta
= forms.DateField()
 paciente
= forms.ModelChoiceField(queryset=Paciente.objects.all())

then you use that pacienteConsultasForm class in your views.py and in your template.

 
On Monday, December 15, 2014 2:36:08 PM UTC-3, Efe wrote:

Efe

unread,
Dec 15, 2014, 8:09:07 PM12/15/14
to django...@googlegroups.com
Excelent!! I´ll try it and I´ll tell you the results. Thanks in advance (sorry for my poor english)
Reply all
Reply to author
Forward
0 new messages