<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{% for post in posts %}
<div class="post-preview">
<a href="{{ url_for('post', post_id=post.id) }}">
<h2 class="post-title">
{{ post.title }}
</h2>
<h3 class="post-subtitle">
{{ post.subtitle }}
</h3>
</a>
<p class="post-meta">Posted by {{ post.author }} on {{ post.date_posted.strftime('%B %d, %Y') }}</p>
</div>
{% if post == posts[-1] %}
<br />
{% else %}
<hr />
{% endif %}
{% endfor %}
Im making a web application on flask and this is the snippet of my code to the articles template I have a sqlite database that contains the articles, how can I make an if statement that detects if the post is the last in the loop since im making an hr per article but not on the last one
{% if post == posts[-1] %} doesn't seem to work.
By Default Flask uses jinja2 as default Template Engine[1] and that's what you're using. Jinja2 provides loop.last variable[2] that you can use as follows:
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{% for post in posts %}
<div class="post-preview">
<a href="{{ url_for('post', post_id=post.id) }}">
<h2 class="post-title">
{{ post.title }}
</h2>
<h3 class="post-subtitle">
{{ post.subtitle }}
</h3>
</a>
<p class="post-meta">Posted by {{ post.author }} on {{ post.date_posted.strftime('%B %d, %Y') }}</p>
</div>
{% if loop.last %}
<br />
{% else %}
<hr />
{% endif %}
{% endfor %}
I'll post both entire form elements even though it might not all be needed to answer the question.
Both buttons are at very end of this code snippet.
I haven't been able to successfully add any row divs, etc. since a form is ending in the middle of the div, and another beginning, it doesn't seem to work.
Any advice?
Thank you.
<div class="row content-section col-md-10">
<form method="POST" action="">
{{ form.csrf_token() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">
{% if sales_order_number %}
<h3>Sales Order Number: {{ sales_order_number }}</h3>
{% else %}
<h3>?? No Sales Order Number ??</h3>
{% endif %}
{% if total_controllers_configured %}
<h3>Total Controllers Configured for this job: {{ total_controllers_configured }} / {{ sales_order_controller_quantity }} </h3>
{% else %}
<h3> Total Controllers Configured for this job: 0 / {{ sales_order_controller_quantity }} </h3>
{% endif %}
<br>
Configure Controller {{ current_controller }} Below:
</legend>
<!-- Fan Quantity and Modbus Ranges -->
<div class="row">
<div class="form-group col-md-8">
{{ form.modbus_ranges.label(class="form-control-label") }}
{% if form.modbus_ranges.errors %}
{{ form.modbus_ranges(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.modbus_ranges.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.modbus_ranges(class="form-control form-control-lg", id="modbus-ranges") }}
{% endif %}
</div>
<div class="form-group col-md-4">
{{ form.fan_quantity.label(class="form-control-label") }}
{% if form.fan_quantity.errors %}
{{ form.fan_quantity(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.fan_quantity.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.fan_quantity(class="form-control form-control-lg" , id="fan-quantity") }}
{% endif %}
</div>
</div>
<!-- fan type, controller type, serial number -->
<div class="row">
<div class="form-group col-md-6">
{{ form.controller_type.label(class="form-control-label") }}
{% if form.controller_type.errors %}
{{ form.controller_type(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.controller_type.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.controller_type(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group col-md-6">
{{ form.controller_serial_number.label(class="form-control-label") }}
{% if form.controller_serial_number.errors %}
{{ form.controller_serial_number(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.controller_serial_number.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.controller_serial_number(class="form-control form-control-lg") }}
{% endif %}
</div>
</div>
<div class="form-check">
{{ form.gfa_fire_NC(class="form-check-input") }}
{{ form.gfa_fire_NC.label(class="form-check-label") }}
</div>
<div class="form-group text-left col-md-6">
{{ form.save_configuration(class="btn btn-outline-info") }}
</div>
</fieldset>
</form>
<form action="" method="POST" novalidate>
{{ form.csrf_token() }}
<div class="form-group text-right col-md-6">
<input type='submit' class="btn btn-outline-danger" name='clear_configuration' value="Clear Configuration"></input>
<!-- {{ form.clear_configuration(class="btn btn-outline-danger", required=False) }} -->
</div>
</form>
I have a problem on a custom widget for a RadioField Wtforms used inside a Flask app.
The custom widget is rendered well, but it DOES NOT WORK because it does not set the "checked" on the field after choosing it. If instead I use the default widget everything is fine and it works.
I also tried another more standard way, that is the "real" custom widget called inside the form class definition ... but I have the exact same result.
Below is the code, any suggestions? Thanks :-)
#### ROUTE
bp = Blueprint('auth', __name__, url_prefix='/auth')
#bp.route('/generic_crud', methods=('GET', 'POST'))
#login_required
def generic_crud():
form = forms.BaseGenericForm()
if form.validate_on_submit():
# Insert the msg
models.BaseGenericFormTable.add_record( title_select = form.titleSelect.data,
title_radio = form.titleRadio.data,
title_checkbox = form.titleCheckbox.data )
return render_template('sample_wtforms/success.html')
return render_template('sample_wtforms/crud_generic.html', form=form)
#### FORM
class BaseGenericForm(FlaskForm):
titleSelect = SelectField( 'Title Select',
validators=[DataRequired(message='Title Select missed')],
choices=[ ('farmer' , 'Farmer'),
('politician', 'Corrupt Politician'),
('cop', 'No-nonsense City Cop'),
('rocket', 'Professional Rocket League Player'),
('lonely', 'Lonely Guy At A Diner'),
('pokemon', 'Pokemon Trainer')],
render_kw={"class":"custom-select"}
)
titleRadio = RadioField('Title Radio',
validators=[DataRequired(message='Title RadioBox missed')],
choices=[ ('farmer' , 'Farmer'),
('politician', 'Politician'),
('cop', 'No-nonsense Cop'),
('lonely', 'Lonely Guy'),
('pokemon', 'Trainer')]
)
titleCheckbox = RadioField('Title Checkbox',
validators=[DataRequired(message='Title CheckBox missed')],
choices=[ ('farmer' , 'Farmer'),
('politician', 'Politician'),
('cop', 'No-nonsense Cop'),
('lonely', 'Lonely Guy'),
('pokemon', 'Trainer')]
)
submit = SubmitField( 'Submit',
render_kw={"class":"btn btn-primary"}
)
#### TEMPLATE jinja2
{% block content %}
<form method="POST" class="container">
<!-- titleSelect -->
<div class="form-group row">
{{ form.titleSelect.label(class='col-4 col-form-label') }}
<div class="col-8">
{{ form.titleSelect }}
{% if form.titleSelect.errors %}
<div class="alert alert-danger" role="alert">
<ul class="errors">
{% for error in form.titleSelect.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
</div>
</div>
<!-- titleRadio -->
<div class="form-group row">
{{ form.titleRadio.label(class='col-4 col-form-label') }}
<div class="col-8">
{#{{ form.titleRadio }}#}
{% for value, label, selected in form.titleRadio.iter_choices() %}
<div class="custom-control custom-radio custom-control-inline">
<input id="radio{{ loop.index0 }}" name="titleRadio" type="radio" class="custom-control-input" value="{{ value }}" {% if selected %} checked="checked" {% endif %}>
<label for="radio{{ loop.index0 }}" class="custom-control-label">{{ label }}</label>
</div>
{% endfor %}
{% if form.titleRadio.errors %}
<div class="alert alert-danger" role="alert">
<ul class="errors">
{% for error in form.titleRadio.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
</div>
</div>
<!-- titleCheckbox ################################################ -->
<div class="form-group row">
{{ form.titleCheckbox.label(class='col-4 col-form-label') }}
<div class="col-8">
{#{{ form.titleCheckbox }}#}
{% for value, label, selected in form.titleCheckbox.iter_choices() %}
<div class="custom-control custom-checkbox custom-control-inline">
<input id="checkbox{{ loop.index0 }}" name="checkbox" type="checkbox" class="custom-control-input" value="{{ value }}" {% if selected %} checked="checked" {% endif %}>
<label for="checkbox{{ loop.index0 }}" class="custom-control-label">{{ label }}</label>
</div>
{% endfor %}
{% if form.titleCheckbox.errors %}
<div class="alert alert-danger" role="alert">
<ul class="errors">
{% for error in form.titleCheckbox.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
</div>
</div>
{{ form.csrf_token }}
<div class="form-group row">
<div class="offset-4 col-8">
{{ form.submit }}
</div>
</div>
</form>
{% endblock %}
For a custom widget I do:
<div id="{{ field.id }}">
{{ field.label(class="form-label") }}<br>
{% for subfield in field %}
<div class="form-check form-check-inline">
<input {% if subfield.checked %}checked {% endif %}type="radio" class="form-check-input" id="{{ subfield.id }}" name="{{ field.id }}" value="{{ subfield.id[-1] }}">
{{ subfield.label(class="form-check-label") }}
</div>
{% endfor %}
</div>
The relevant part for you should be "{% if subfield.checked %}checked {% endif %}"
I have the following code in one of my templates. As you can see, there is a lot of repetition going on. Hence, I am wondering if I can somehow use Django Template to consolidate this code while achieving the same (or closely comparable) result when it comes to HTML. Namely, I am interested if I can sort the todo entries into two different <ul> tags on the page, depending on the boolean value of todo.todo_completed.
{% block content %}
{% if todo_list %}
<ul class="list-group">
{% for todo in todo_list %}
{% if not todo.todo_completed %}
<li class="list-group-item">
<div class="row">
<div class="col-sm-5">
<a href="{% url 'list:todo-detail' todo.id %}" >{{ todo.todo_name }}</a>
</div>
<div class="col-sm-6">
</div>
<div class="col-sm-1">
{% bootstrap_icon "ok" %}
{% bootstrap_icon "remove-circle" %}
</div>
</div>
</li>
{% endif %}
{% endfor %}
</ul>
<ul class="list-group">
{% for todo in todo_list %}
{% if todo.todo_completed %}
<li class="list-group-item">
<div class="row">
<div class="col-sm-5">
{{ todo.todo_name }}
</div>
<div class="col-sm-6">
</div>
<div class="col-sm-1">
{% bootstrap_icon "ok" %}
{% bootstrap_icon "remove-circle" %}
</div>
</div>
</li>
{% endif %}
{% endfor %}
</ul>
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 %}