Reputation: 730
So I have django model choice field with months
class EntrySummary(models.Model):
JANUARY = '1'
FEBRUARY = '2'
MARCH = '3'
APRIL = '4'
MAY = '5'
JUNE = '6'
JULY = '7'
AUGUST = '8'
SEPTEMBER = '9'
OCTOBER = '10'
NOVEMBER = '11'
DECEMBER = '12'
MONTH_CHOICES = (
(JANUARY, 'January'),
(FEBRUARY, 'February'),
(MARCH, 'March'),
(APRIL, 'April'),
(MAY, 'May'),
(JUNE, 'June'),
(JULY, 'July'),
(AUGUST, 'August'),
(SEPTEMBER, 'September'),
(OCTOBER, 'October'),
(NOVEMBER, 'November'),
(DECEMBER, 'December'),
)
name = models.CharField(
max_length=255
)
month = models.CharField(
max_length=2,
choices = MONTH_CHOICES,
default=JANUARY,
)
Views.py I render it
def based_onmonth(request, *args, **kwargs):
monthId = request.GET.get('month')
monthEntry = EntrySummary.objects.filter(month=monthId)
return render(request, 'app/js_templates/month_dropdown_list.html', {'MonthItem': monthId})
html template:
<option value="">---------</option>
{% for item in MonthItem %}
<p>item.name</p>
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
Doing this i receive {'month': '2'} in my select section
My question is how to print just 2 without month
Upvotes: 0
Views: 854
Reputation: 14360
Use the get_<field_name>_display
mechanism:
# Return the right thing from your view.
return render(request, 'app/js_templates/month_dropdown_list.html', {'MonthItem': monthEntry})
and in your template:
{% for item in MonthItem %}
<option value="{{ item.month }}">{{ item.get_month_display }}</option>
{% endfor %}
Reference: https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.get_FOO_display
Upvotes: 1
Reputation: 101
In your return you will want to send the monthEntry
to the frontend:
return render(request, 'app/js_templates/month_dropdown_list.html', {'MonthItem': monthEntry})
Upvotes: 0