How to populate table after button click in django template? - python

1) I want to insert the values in the input field from the dictionary using DTL.
Here's the snippet of code that on load page, making invisible this code tag in the table (may be because of if condition turns False) but just after data submission from dictionary, showing inserted value in the same tag.
How can I make it work and remains all the td tags visible on the page load as well?
2) Also since my data.items having 6 keys, so its iterating 6 times, I just want to get single time.
I know I'm not very clear but sorry I can't post the whole code since is too big and confidential.
Please Help it out, I'm totally new in django. Thanks.
....
...
..
{% for key, value in data.items %}
{% for key2,value2 in value.items %}
<tr class="info">
<td>1</td>
<td>Cholesterol -HDL</td>
<td>
{% if value2.test_name == "Cholesterol -HDL" %}
<div class="form-group">
<input type="text" class="form-control" name="cholesterol_hdl_result" value="{{ value2.results }}">
</div>
</td>
<td><div class="form-group">
<input type="text" class="form-control" name="cholesterol_hdl_uom" value="{{ value2.units }}">
</div></td>
<td><div class="form-group">
<input type="text" class="form-control" value="40.00" name="cholesterol_hdl_lr">
</div></td>
<td><div class="form-group">
<input type="text" class="form-control" value="60.00" name="cholesterol_hdl_hr">
</div></td>
{% endif %}
</tr>
{% endfor %}
{% endfor %}
..
...
....

So, when the dictionary is empty you have to check that in if and else condition and write that <td> tag in else block as below...
{% if data %}
......
......
{% for key, value in data.items %}
{% for key2,value2 in value.items %}
....
....
{% endfor %}
{% endfor %}
{% else %}
# write your default <td> tag here which is shown when there is empty data dictionary
{% endif %}
And for iterate for loop only once you have to use forloop.first of django template as below...
{% for key, value in data.items %}
{% if forloop.first %}
{% for key2,value2 in value.items %}
....
....
{% endfor %}
{% endif %}
{% endfor %}

Related

Html content not showing up after if statement in Django

Im having some issues with my html content not showing up after putting in some {% if %} {% endif %} statements in my templates
I have this snippet in my code where I display the checkout table if and only if the current user's username matches the one from my Order model. (order.customer_name has a foreign key that is set to the current users username)
{% for order in latest_order %}
{% if user.username == order.customer_name %}
<tr>
<td>{{ order.order_name }}</td>
<td>
<form method="post">
{% csrf_token %}
<button type="submit" name="remove_quantity" value="{{ order.id }}" class="mr-3 btn
btn-outline-info">-</button>
{{ order.order_quantity }}
<button type="submit" name="add_quantity" value="{{ order.id }}" class="ml-3 btn btn-
outline-info">+</button>
</form>
</td>
<td>${{ order.order_individual_price }}</td>
<form method="post">
{% csrf_token %}
<th><button type="submit" name="delete" value="{{ order.id }}" class="btn btn-
danger">Delete Item</button></th>
</form>
</tr>
{% endif %}
{% endfor %}`
I tried the following to see if it prints out the same username, which it does
<h1>{{ user.username }}</h1>
{% for order in latest_order %}
<h1>{{ order.customer_name }}</h1>
{% endfor %}
Picture of the page with user.username and order.customername as h1
When I delete the if statement, this is what the website SHOULD look like
Proper working table
Im pretty sure I'm missing something very obvious, Any help is appreciated!

Learning to use Jinja2 templates, missing URL values

I'm learning how to use Jinja2 templates (see code below). When I add an item to my form, I expect that the url will change to something like this.
http://localhost:8080/?food=steak&food=eggs&food=cheese
However, what ends up happening is that the first food will have a value, but everything else will be blank. It looks something like this:
http://localhost:8080/?food=asd&food=&food=
What am I doing wrong?
<form>
<h2>Add a Food</h2>
<input type="text" name="food">
{% if items %}
{% for items in items %}
<input type="hidden" name="food" value="{{item}}">
{% endfor %}
{% endif %}
<button>Add</button>
{% if items %}
<br>
<br>
<h2>Shopping List</h2>
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endif %}
</form>
Here is the function to render the HTML:
class MainPage(Handler):
def get(self):
items = self.request.get_all("food")
self.render("shopping_list.html", items=items)
For item in items?
{% for item in items %}
<input type="hidden" name="food" value="{{item}}">
{% endfor %}
I'm not certain, but I think the issue is with your <button> tag. Try something like <button type='submit'>. I think your current button just doesn't do anything.
w3 documentation here

Variable inside a forloop in Python Django

am new to Python and Django.
I have a problem. i want to increment the value of current inside the for loop and check if the value is equal to 1 and if not i want to display some HTML tags.
here is my code.
{% with current=1 %}
{% for howitwork in howitworks%}
{% if current != 1 %}
<div class=item>
<div class=container-fluid>
<div class=row>
<div class="col-md-5 rex-block">
<img src="media/{{ howitwork.image }}" alt>
</div>
<div class="col-md-7 rx-services-box">
<div class=rx-conta-title>
<h3>{{ howitwork.subtitle }}</h3>
</div>
<p>{{ howitwork.description|linebreaks }}</p>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% endwith %}
I don't see any errors but its not working.
If you need to skip the logic on first loop iteration just use forloop.first field. It is accessible inside forloop block.
{% for howitwork in howitworks %}
{% if not forloop.first %}
<!-- do stuff -->
{% endfor %}
Use forloop.counter (forloop.counter0 for 0-indexed) instead of current variable.
Here is the possible solution for your snippet:
{% for howitwork in howitworks%}
{{forloop.counter}}
{% if forloop.counter != 1 %}
<div class=item>
<div class=container-fluid>
<div class=row>
<div class="col-md-5 rex-block">
<img src="media/{{ howitwork.image }}" alt>
</div>
<div class="col-md-7 rx-services-box">
<div class=rx-conta-title>
<h3>{{ howitwork.subtitle }}</h3>
</div>
<p>{{ howitwork.description|linebreaks }}</p>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
To check the current iteration count, you can use {{forloop.counter}}
So your condition could be like this
{% if forloop.counter == 1 %}
<!-- Do things -->
{% else %}
<!-- Do some other things -->
{% endif %}

Template logic not working - Django

template
<form method="post" action=".">
{% csrf_token %}
<table width="100%" cellpadding="0" cellspacing="0" >
<tr>
<td colspan="2" class="incident-type">
{% for type in typeList%}
{% if type.parent_type_id == None %}
<h1>{{type.title}}</h1>
{% else %}
{% if checked_ones %}
<label><input type="checkbox" checked="True" value="{{ type.title }}" name="key">{{ type.title }}</label><br />
{% else %}
<label><input type="checkbox" value="{{ type.title }}" name="key">{{ type.title }}</label><br />
{% endif %}
{% endfor %}
see here,check box inputs are getting from this line in views.py
checked_ones = [unicode(x) for x in subtype if unicode(x) in request.POST.getlist('key')]
The Problem here is,if any one of the option is checked and saved,all the other checkbox options are getting selected(after page redirect).
Tried with some logic loop,Need some help.
Try this,
{% for type in typeList%}
{% if type.parent_type_id == None %}
<h1>{{type.title}}</h1>
{% else %}
{% if type.title in checked_ones %}
<label><input type="checkbox" checked="True" value="{{ type.title }}" name="key">{{ type.title }}</label><br />
{% else %}
<label><input type="checkbox" value="{{ type.title }}" name="key">{{ type.title }}</label><br />
{% endif %}
{% endif %}
{% endfor %}
Hope this helps!

Unique HTML element IDs for each model with Django

I've got django models displaying on a page using a checkbox input that has a label over it like so:
{% if recipes_list %}
<table>
{% for r in recipes_list %}
<tr>
<td>
<section class="ac-container">
<div>
<input id="ac-1" type="checkbox" />
<label for="ac-1">{{r.name}}</label>
<article class="ac-small">
<ul>
{% for i in r.ingredient_list%}
<li>{{i.part}}, {{i.amount}}</li>
{% endfor %}
</ul>
</article>
</div>
</section>
</td>
</tr>
{% endfor %}
</table>
When I click on the label of each entry in recipes_list, it obviously always opens the article of the first one. I've been looking around for solutions for the past couple days on how to give a unique id in the html for each model entry, but I can't find anything that works with this situation. I've tried forms, model forms, various javascript, and php. How can I do this?
You can use the forloop.counter to achieve this:
{% if recipes_list %}
<table>
{% for r in recipes_list %}
<tr>
<td>
<section class="ac-container">
<div>
<input id="ac-{{forloop.counter}}" type="checkbox" />
<label for="ac-{{forloop.counter}}">{{r.name}}</label>
<article id="article-{{forloop.counter}}" class="ac-small">
<ul>
{% for i in r.ingredient_list%}
<li>{{i.part}}, {{i.amount}}</li>
{% endfor %}
</ul>
</article>
</div>
</section>
</td>
</tr>
{% endfor %}
</table>
Hope this helps!
You can write a filter which gets the model name
from django import template
register = template.Library()
#register.filter(name='class_name')
def class_name(obj):
return obj.__class__.__name__
and in the template:
and in the template, wherever you want the id/classname:
<article id={{obj|class_name}}>
{# further code #}
</article>
OR
class MyModel(models.Model):
#fields
def class_name(self):
return "%s"%self.__class__.__name__ #returns the model instance name
If you want to return the instance name:
from django.template.defaultfilters import slugify
class MyModel(models.Model):
def class_name(self):
return "%s"%(slugify(self.name)) #or whatever field has the specific instance name
and in the template:
{{obj.class_name}}
Its simple use object primary key as id because its unique (unless you have another loop from another model):
{% for r in recipes_list %}
<input id="ac-{{ r.id }}" type="checkbox" />
{% endfor %}
Or use forloop.counter:
{% for r in recipes_list %}
<input id="ac-{{ forloop.counter }}" type="checkbox" />
{% endfor %}

Categories

Resources