Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Django django_filters DateFilter customization

243 views
Skip to first unread message

Mayank Tripathi

unread,
Jan 22, 2021, 3:34:55 PM1/22/21
to Django users
Hi All,

I am look for a solution where I can restrict the user to select date from current month only instead of selecting any other month or year.

Currently user can select past month as well, have to restrict for current month only in date picker.

Please help.

Below is the code.
filters.py file screenshot is as below.


datepicker.PNG
filters py file.PNG

Kasper Laudrup

unread,
Jan 22, 2021, 3:47:10 PM1/22/21
to django...@googlegroups.com
Hi Maynak,

On 22/01/2021 21.34, Mayank Tripathi wrote:
> Hi All,
>
> I am look for a solution where I can restrict the user to select date
> from current month only instead of selecting any other month or year.
>
> Currently user can select past month as well, have to restrict for
> current month only in date picker.
>

It seems like the HTML date input field has attributes for setting the
min and max value:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#additional_attributes

Setting these attributes to the first and last day of the month for the
widget ought to work:

https://docs.djangoproject.com/en/3.1/ref/forms/widgets/#django.forms.Widget.attrs

Of course, you still need to ensure that the client doesn't send an
invalid/unexpected date.

I haven't tested this, but that's what I'd try to do at least.

Hope that helps.

Kind regards,

Kasper Laudrup

_M_A_Y_A_N_K_

unread,
Jan 22, 2021, 4:32:16 PM1/22/21
to django...@googlegroups.com
Thanks Kasper,

Will try the suggested option. 

Meanwhile, do you know how to get the documentation for danjo_filters DateFilter() method, and want to know what all parameters it can accept. Usually we do pass  field_name; lookup_expr; label , just curious if we could pass attrs as we pass in forms.Modelform.
I did try checking the document from https://django-filter.readthedocs.io/en/stable/ref/filters.html#datefilter, but there its not mentioned. 
Please direct me to the right direction.


Thanks & Regards,
---------------------
Mayank Tripathi
Mo. +1 615 962 2128
"Do what you can, with what you have, where you are -by Theodore Roosevelt"



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c56ecb88-6007-107c-e52f-c72f09f05f2a%40stacktrace.dk.

_M_A_Y_A_N_K_

unread,
Jan 22, 2021, 5:44:15 PM1/22/21
to django...@googlegroups.com
Thanks Kasper,

I utilized the suggestion you shared, and implemented it with a custom template tag, and now am able to restrict it.

image.png


Thanks & Regards,
---------------------
Mayank Tripathi
Mo. +1 615 962 2128
"Do what you can, with what you have, where you are -by Theodore Roosevelt"


On Fri, Jan 22, 2021 at 2:46 PM Kasper Laudrup <lau...@stacktrace.dk> wrote:

Kasper Laudrup

unread,
Jan 23, 2021, 5:39:07 AM1/23/21
to django...@googlegroups.com
Hi Mayank,

On 22/01/2021 23.43, _M_A_Y_A_N_K_ wrote:
> Thanks Kasper,
>
> I utilized the suggestion you shared, and implemented it with a custom
> template tag, and now am able to restrict it.
>

You are very welcome. Glad to hear it worked for you and thanks for
sharing your solution.

All the best.

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages