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!
Related
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 %}
Here is the code I'm trying to run in my Flask App
{% extends "bootstrap/base.html" %}
{% block title %}Testing title{% endblock %}
{% block content %}
<div class="container">
<h1>Posts</h1>
<h3>Postings</h3>
<form action="/" method="post">
<input hidden placeholder="Name" name="name">
<input placeholder="Post whatever you want..." name="post" required>
<button class="btn btn-primary" type="submit" value="Submit">Submit</button>
</form>
{% for post in posts reversed %}
<div>
{{ 'Anonymous' + ': ' + post[2] }}
</div>
{% endfor %}
</div>
{% endblock %}
This is the error I get jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 'reversed'
Figure reversed would work since I found some examples online that does that
The correct syntax in Jinja2 to loop through a list in reverse order is:
{% for post in posts|reverse %}
{{ post }}
{% endfor %}
I am using django_markdown but it is showing error when I am trying to render form. The error is on 17th line {{ form }} Error. The error is in reverse of 'django_markdown_preview', but I have included
url('^markdown/', include('django_markdown.urls')),
in my urls.py. Can anyone help me to resolve the error.
{% extends 'qaforum/base.html' %}
{% load django_markdown %}
{% block content %}
{% if message %}
<strong>Enter a valid Question!</strong>
{% endif %}
<form method="post" action="{% block action_url %}{% url 'qaforum:qaforum_create_question' %}{% endblock action_url %}">
{% csrf_token %}
{% for field in form.visible_fields %}
<tr>
<th>{{ field.label_tag }}</th>
<td>
{{ field.errors }}
{{ field }}
{{ field.help_text }}
</td>
</tr>
{% endfor %}
<input class="btn pull-right btn-success" type="submit" value="Submit Question" />
</form>
</div>
{% endblock content %}
{% block extra_js %}
{% markdown_editor "#id_description" %}
{% markdown_media %}
{% endblock extra_js %}
I have a form in django:
country=forms.MultipleChoiceField(choices=lista_tari, widget=forms.CheckboxSelectMultiple(),required=True)
What i want is to display this form on multiple columns in the webpage. There are 30 choices, i want them on 6 or 5 or whatever columns.
This is the search.html:
{% block content%}
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}
I am brand-new to css, html and even django so any help would be of great help.
Thank you!
{% for field in form %}
{% ifequal forloop.counter 6 %}</ul><ul>{% endifequal %}
<li>{{ field }}</li>
{% endfor %}
you can try something like this
<form method="POST" class="post-form">{% csrf_token %}
<div class="form-check form-check-inline">
{% for field in form %}
{% if forloop.counter|divisibleby:3 %}</div><div class="form-check form-check-inline">{% endif %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
</div>
<button type="submit" class="save btn btn-default">Save</button>
</form>
But probably you should use django-bootstrap3 which helps you to integrate django and bootstrap.
I'm trying to make a list of buttons, each corresponding to an object in my database. However, when I press one of them, it is always the value of the last button generated which is sent back to the view:
{% if segmenter %}
<form method="post">
{% csrf_token %}
<ul>
{% for segment in segments %}
<li>
<input type="hidden" name="id" value="{{ segment.pk }}"/>
<button type="submit">{{ segment }}</button>
</li>
{% endfor %}
</ul>
{% else %}
<strong>No segments registered. </strong><br />
{% endif %}
Form items are distinguished by name but you're using the same name for your inputs. Besides using one form means you intend to send all the inputs when the inputs are differentiated.
You should do:
{% if segmenter %}
{% csrf_token %}
<ul>
{% for segment in segments %}
<form method="post">
<li>
<input type="hidden" name="id_{{ segment.pk }}" value="{{ segment.pk }}"/>
<button type="submit">{{ segment }}</button>
</li>
</form>
{% endfor %}
</ul>
{% else %}
<strong>No segments registered. </strong><br />
{% endif %}
which posts a separate form on click of each button