2

I want to create a year dropdown in django template. The dropdown would contain start year as 2011, and end year should be 5 years from current year.

For ex: If today i see the dropdwon it will show me years ranging from 2011, 2012, 2013,...2017.

It can be done by sending a list from views.py and looping it in the template or defining inside forms.py.

But i don't want to use any form here. I just have to show it in a template.

How can i do it in Django template.

Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
sandeep
  • 3,061
  • 11
  • 35
  • 54

5 Answers5

8

In your models.py...

import datetime

year_dropdown = []
for y in range(2011, (datetime.datetime.now().year + 5)):
    year_dropdown.append((y, y))

So, your field can now use year_dropdown:

year = models.IntegerField(_('year'), max_length=4, choices=year_dropdown, default=datetime.datetime.now().year)

Which sets the current year as the default value.

If all you want is the values in the template, then do something like the following...

<select id="year">
{% for y in range(2011, (datetime.datetime.now().year + 5)) %}
    <option value="{{ y }}">{{ y }}</option>
{% endfor %}
</select>

For more details, read the Django template language documentation.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • The question mentions that he don't want to use forms. – Babu Jan 04 '13 at 07:03
  • Hey Ian i don't want to use any kind of views, models or forms here, coz the data is not going to be saved any where. I just want to display the dropdwon and upon selecting the year it will pull out the data for that year from db. So can we do it from the client side itself. – sandeep Jan 04 '13 at 07:06
  • Ian by adding ur above code in my template(html page), i got this error message -> 'for' statements should use the format 'for x in y': for y in range(2011, (datetime.datetime.now().year + 5)) – sandeep Jan 04 '13 at 07:23
  • Oops, I included the `:` and I think that's the cause of your error. See the colon at the end of the `if` statement? Remove that, see what happens. – Ian Atkin Jan 04 '13 at 07:27
  • Then it must be the case that the templating language can't deal with complex syntax such as a `range`. Sorry. – Ian Atkin Jan 04 '13 at 07:30
  • 2
    Hi Gordon, I like your first answer. Using a similar approach, I ran into a case where my server didn't restart for months and thus my selection of years did not update. Could you please remove your second answer as you cannot run normal python code in a Django template and your text is misleading now? Thanks! – Wim Feijen Jan 17 '17 at 14:13
  • 'max_length' is ignored when used with IntegerField. – Brendan Metcalfe Nov 27 '19 at 02:45
1

If all you want is a simply a drop down of years itself, you can do what @Nick or @Ian suggested. In case you want some calendar type functionality (Date picker) you can have a look at JQuery UI DatePicker

Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
0
<select name="select_year">
<option>2011</option>
<option>2012</option>
<option>2013</option>
<option>2014</option>
<option>2015</option>
<option>2016</option>
<option>2017</option>
</select>

put that in your template file ... if you want to do a dynamic range of years you will need to make your own template tag

this thread has several answers for implementing range type functionality as a template tag

Numeric for loop in Django templates

using a javascript solution would probably be the easiest way to dynamically set the range.

Community
  • 1
  • 1
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • No i can't do this, coz when 2014 comes i have to show years ranging from 2011, 2012,2013,2014...2018. Can u pls suggest how can i get it done by writing a template tag. Thanks – sandeep Jan 04 '13 at 07:04
0

This is how I like to do it.

models.py:

YEAR_CHOICES = []
for r in range(2011, (datetime.datetime.now().year+5)):
    YEAR_CHOICES.append((r,r))

year = models.IntegerField(('year'), max_length=4, choices=YEAR_CHOICES, default=datetime.datetime.now().year)
Nick
  • 474
  • 1
  • 3
  • 14
  • I don't want to use any server side code here. Can we do it in the template itself. – sandeep Jan 04 '13 at 07:07
  • If you dont want any server-side code here, then this question isnt really about using django. You can easily create a select box using only html or jquery as someone suggested. But Im not sure the purpose of having users input data if it is not saved to the database.. – Nick Jan 04 '13 at 09:18
  • If, on the other hand, you simply want to keep this out of your models and views files, we could talk about creating a templatetag for this process. – Nick Jan 04 '13 at 09:23
  • Its not an user input to the database, its a drop down used for pulling data from the db. Thus no need of using a model or form. Yeah you are right about using template tags, that will help for sure. But i am a new dev in Django, so dont have much idea about writing template tags. If u can help for writing a template tag that would be great.:) – sandeep Jan 04 '13 at 13:50
0

I would suggest to use Javascript or jQuery at client side to populate the dropdown according to the current year.

Babu
  • 2,548
  • 3
  • 30
  • 47