python - onclick auto executing in djnago template -
i'm having little trouble python/django web app. i'm trying able enable , disable in admin area in front end either show or not.
once link clicked want function called updatevisibilitymain execute passing in main.id. issue auto fires on page loads along if click 1 link 3 fire , update each main item respectively according main.id. want 1 link fire @ time along not having auto fire on page load.
my issue link.
<a href="" id="{{main.id}}" onclick="{% updatevisiblitymain main.id %}" class="toggler"><i class="{% isvisible main.is_visible %}" aria-hidden="true"></i></a>
when load page onclick auto called , disables , or enables every page load. on top of have 2 more links similar created in loop. , if click of 3 execute , either disable or enable object. 1 have ideas on how fix this?
i've tried putting # inside href area , onclick function not work when clicked.
i appreciate help.
edit: updatevisibilitymain
@register.simple_tag def updatevisiblitymain(cid): """ updates visibility of main category """ print cid main = maincategory.objects.get(pk=cid) visible = main.is_visible if visible: main.is_visible = false main.save() logger.debug('main category %s has been marked false.' % main.name) else: main.is_visible = true main.save() logger.debug('main category %s has been marked true.' % main.name)
django template
<div class="row"> {% if sub_cats %} {% sub in sub_cats %} {% if sub.main_category_id == main_id %} <div class="pull-left pad-10"> {% main in main_cats %} {% if main.id == main_id %} <a href="" id="{{main.id}}" onclick="{% updatevisiblitymain main.id %}" class="toggler"><i class="{% isvisible main.is_visible %}" aria-hidden="true"></i></a> <span class="form-section">{{ main.name }}</span> <a href="" class="gutter"><i class="{% helptext main.help_text %}" aria-hidden="true"></i></a> <a href="/mfa/deleteitem/maincat/{{main.id}}" class="gutter"><i class="fa fa-lg fa-trash-o red" aria-hidden="true"></i></a> {% endif %} {% endfor %} </div> {% endif %} {% endfor %} {% endif %} </div>
you have not understood distinction between front-end , back-end code.
onclick
javascript event handler. should contain js code executes on frontend.
django template language executes on backend. called @ render time, when user first requests page, not when interact it.
the way write js function uses ajax call django view updates visible status.
Comments
Post a Comment