jinja2 is not recognizing {% endfor %} and runs into this error:
TemplateSyntaxError at endfor in jinja django coding
Below is the code snippet:
{% for dest in dests % }
<!-- Destination -->
<div class="destination item">
<div class="destination_image">
<img src="{{im}}/{{dest.img}}" alt="">
<div class="spec_offer text-center">Special Offer</div>
</div>
<div class="destination_content">
<div class="destination_title">{{dest.name}}</div>
<div class="destination_subtitle"><p>{{dest.desc}}</p></div>
<div class="destination_price">From ${{dest.price}}</div>
</div>
</div>
{% endfor %}
Here dest is a list which is displaying the content.
It is not recognizing the forloop because your jinja formatting is wrong.Cannot have space at start and end of percentage.Change to this{% for dest in dests %}.
when you are getting images for database you need .url at the end. So you code should look like <img src="{{dest.img.url}}" alt="">
Related
When you create a page inside the admin panel on Wagtail, it says to you how many time it has passed since then.
I want to put the time stamp of the creation of the page inside a paragraph on the HTML, like this:
This is my code:
<div class="contenedor tarjetas mt-4 mb-4">
{% for post in posts %}
<div class="fila_tarjetas clearfix">
<div class="col_tarjeta_img">
{% image post.blog_image fill-450x450 as blog_img %}
<a href="{{ post.url }}">
<img class="tarjeta_imagen" src="{{ blog_img.url }}" alt="{{ blog_img.alt }}">
</a>
</div>
<div class="col_tarjeta_texto">
<a class="tarjeta_titulo" href="{{ post.url }}">
<h3>{{ post.custom_title }}</h3>
<div class="tarjeta_contenido"> {{ post.content }} </div>
Leer más > >
</a>
</div>
</div>
{% endfor %}
</div>
The date you want is probably available as post.last_published_at. If you want to display it as an absolute date, you can use the Django template tags for formatting dates, for example {{ post.last_published_at|date:"F d, Y" }} Or if you want a relative date, use the timesince filter
code snapshot for my code error which is getting by flask jinja2 while using into html to get images by giving url through jinja2
{% for book in books %}
<div class="swiper-slide box">
<div class="icons">
</div>
<div class="image">
<img src="static/image/{{book['img']}}" alt="{{book['img']}}" />
</div>
<div class="content">
Read
</div>
</div>
{% endfor %}
I think it's to do with how you are accessing your static files, try in the format:
{{ url_for('static', filename='images/image_name.jpg') }}
https://flask.palletsprojects.com/en/1.1.x/tutorial/static/
I see, try:
<img src="static/image/{{book.img}}" alt="{{book.img}}" />
I want to pass in an Expression into another expression in a flask jinja2 for Loop to Load an image page
{% for item in items %}
{% include 'includes/items_modals.html' %}
<div class="card-deck" style="width:250px; height:350px">
<div class="card bg-dark" style="width: 18rem; margin-top:20px">
<img src="{{url_for('static', filename='images/{{item.img_path}}')}}" class="card-img-top" alt="...">
<div class="card-body">
<h4 class="card-title" >{{item.name}}</h4>
<p class="card-text" style="color:green; font-weight:bold">₦{{item.price}}</p>
Add to Cart
</div>
</div>
</div>
{% endfor %}
I'm working on a simple Django project with bootstrap. I have a set of cards being displayed on a page, and the cards are showing just fine (Don't mind the place holder images).
I've been reading the bootstrap docs about cards, but since I have less knowledge of CSS than I'd like, I can't figure out how to simply change the size of the cards so they are larger and stacked in rows instead of one column.
This is my HTML template for this page. It just uses a for-loop to go through my projects and make a card for each. You can see I have the title and text worked out, and the URL to 'See Details'.
{% extends "base.html" %}
{% load static %}
{% block page_content %}
<h1>Projects</h1>
<div class="row">
{% for project in projects %}
<div class="col-md-4">
<div class="card mb-2">
<img class="card-img-top" src="{% static project.image %}">
<div class="card-body">
<h5 class="card-title">{{ project.title }}</h5>
<p class="card-text">{{ project.description }}</p>
<a href="{% url 'project_detail' project.pk %}"
class="btn btn-primary">
See Details
</a>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
The Base.html is just linking to the bootstrap and all the html for the top of the page. Hopefully the fix is a really simple addition to the template, but I haven't had much luck finding info.
You can use row to create grid row for each card object, and remove the col div.
{% extends "base.html" %}
{% load static %}
{% block page_content %}
<h1>Projects</h1>
{% for project in projects %}
<div class="row">
<div class="card mb-2">
<img class="card-img-top" src="{% static project.image %}">
<div class="card-body">
<h5 class="card-title">{{ project.title }}</h5>
<p class="card-text">{{ project.description }}</p>
<a href="{% url 'project_detail' project.pk %}"
class="btn btn-primary">
See Details
</a>
</div>
</div>
</div>
{% endfor %}
{% endblock %}
I am trying to render a block of HTML code repeatedly using for loop. But Django throws a TemplateSyntaxError when I reload my browser
<div class="carousel-item active">
{% for number in range(3) %}
<!--Slide {{ number + 1 }}-->
<div class="row">
{% for number in range(6) %}
<!--Slide 1 Col {{ number + 1 }}-->
<div class="col-lg-2">
<div class="card" style="width: 100%;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
I expected a repeated rendering of the blocks inside the for loop but got "TemplateSyntaxError at /
Could not parse the remainder: '(3)' from 'range(3)'" instead
Django does not allow to make function calls (with parameters) as well as subscripting in the templates. The rationale is that business logic should not be part of the template. You thus can pass the range(3) and range(6) object through the context to the template. The {{ number + 1 }} will not work either, since such operators are not supported either.
An alternative is using Jinja, which is a template engine that does allow to use such Python syntax in the templates.
Since the numbers are quite small, a third options is to use string literals instead:
<div class="carousel-item active">
{% for row in '123' %}
<!--Slide {{ row }}-->
<div class="row">
{% for col in '123456' %}
<!--Slide 1 Col {{ col }}-->
<div class="col-lg-2">
<div class="card" style="width: 100%;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
</div>