0

I want to create a simple calendar in html but I can not create FOR 31 times

Django-Views:

def calendar(request):
    try:
        tvserie = Tvserie.objects.order_by('-date_next_episode')
        return render_to_response('calendar.html',{'tvserie':tvserie})
    except:
        return render_to_response('error_page.html')

Django-Template:

<table border="1">
  {% for 0 in 31 %}
    <tr>
    {% if forloop.counter == tvserie.date_next_episode.day %}
      <td>
        {{tvserie.date_next_episode.day}}
        {{tvserie.network}}<br>
        {{tvserie.number_next_episode}} {{tvserie.title}}<br>
        "{{tvserie.title_next_episode}}"<br>
      </td>
    {% else %}
      <td>
        {{forloop.counter}}
      </td>
    {% endif %}
    </tr>
  {% endfor %}
</table>
TZHX
  • 5,291
  • 15
  • 47
  • 56
JacopoWoty
  • 21
  • 3

2 Answers2

0

You can create a custom template filter to create a range in the following way:

from django import template
register = template.Library()

@register.filter
def get_range(val):
    return range(val)

Then, you should replace this:

{% for 0 in 31 %}

With this:

{% for day in 31|get_range %}

It creates a 31 items range and iterates over it.

OrenD
  • 1,751
  • 13
  • 12
  • Django doesn't have a built-in `get_range` template filter. If you refer to a specific custom filter implementation, please provide it – Spc_555 Jun 03 '15 at 11:26
  • How do I create get_range? – JacopoWoty Jun 03 '15 at 11:57
  • @VasilyAlexeev, you're right of course. Forgot I had it implemented and that it doesn't come out of the box with Django.. Updated the answer with the implementation. – OrenD Jun 03 '15 at 14:10
0

One way is use custom tag or simply this may help you.

{% for i in "xxxxxxxxxxxxxxxxxxxx#31 times" %}
    {{ forloop.counter0 }}
{% endfor %}
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49