user3439521
user3439521

Reputation:

Django - How to use an increment counter inside of template

I'm trying to have my form not display (display:none;) if the maximum number of people signed up for a specific event is reached/exceeded. signups is a model with fields eventname and fullname. I'm also using a ListView, FormView to loop through the list of events, each with a sign up form.

I'm trying to do something like:

<form action="/events/" class="form" method="POST" style="{% for signups in signup %}{% if signups.eventname == events.name %}*counter increment here*{% if *counter value* >= events.maximum %}display:none;{% endif %}{% endif %}{% endfor %}" id="{{ events.name }}" name="{{ events.name }}">

{% if signups.eventname == events.name %} checks the model signups for objects with matching eventnames so that only objects for the wanted event is counted. This is all inside of {% for events in events_list %}{% endfor %} and consider text inside asterisks comments.

How would I do this? If you would like to see any other files or information, I will gladly edit this.

Upvotes: 0

Views: 1165

Answers (2)

Pythonista
Pythonista

Reputation: 11615

Think you're going about ths the wrong way. Get the number of people signed up first in your view which you can then pass to your template as a template variable and make an if condition something like this:

{% if (signed_up_variable) < (max number) %}
    # display the form
{% endif %}

Although, if i recall correctly ifequal may be deprecated, so adjust that accordingly.

Upvotes: 3

rsb
rsb

Reputation: 420

i guess you need forloop.counter . https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#for

Upvotes: 0

Related Questions