0

I am learning web development using django framework. I have little trouble in running python code in an html page. I need to iterate through a list of strings which basically contains the paths of images that I want to show in a carousel. I am getting this error "Invalid block tag on line 83: 'with.i', expected 'endblock'. Did you forget to register or load this tag?" in galleryPage.html file when I try to open that page. These are the few lines of code that I've written so far

/views.py/

def galleryPage(request):
    from os import listdir
    from os.path import isfile, join

    ImageList = []
    imagePath = "static/images/gallery/"

    for f in listdir(imagePath):
        temp = join(imagePath, f)
        if isfile(temp):
            temp = "../" + temp
            ImageList.append(temp)

    return render(request, "galleryPage.html", {"imageList": ImageList})

/galleryPage.html/

{% extends 'base.html' %}

{% block title %} GALLERY {% endblock %}

{% block slideshow %}

* {box-sizing: border-box;}
body {font-family: Verdana, sans-serif;}
.mySlides {display: none;}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active {
  background-color: #717171;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .text {font-size: 11px}
}

{% endblock %}

{% block content %}

<div class="slideshow-container">

{% with.i = 0 %}
<indent>
{{ for image in imageList.0: }}
    {%  ++i %}
    <div class="mySlides fade">
    <div class="numbertext"> {% i %} / {{ imageList.size() }} </div>
    <img src="{{ image }}" style="width:100%">
    <div class="text">Caption Text</div>
    </div>
</indent>
{% endwith %}
</div>
<br>

<div style="text-align:center">
  <span class="dot"></span> 
  <span class="dot"></span> 
  <span class="dot"></span> 
</div>

<script>
var slideIndex = 0;
showSlides();

function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}    
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}
</script>

{% endblock %}
0

1 Answer 1

2
{% with.i = 0 %}

should be

{% with i=0 %}

Why do you have imageList.0 instead of imageList?

Also, I think you may want to consider combining your current "with" and "for" into one "for" loop like in the docs I posted above:

{% for key, image in imageList %}
   <div class="mySlides fade">
   <div class="numbertext"> {% key %} / {{ imageList.size() }} </div>
   <img src="{{ image }}" style="width:100%">
   <div class="text">Caption Text</div>
   </div>
{% endfor %}

This way you won't need to increment i yourself and you take away some complexity (and some sources of errors).

Sign up to request clarification or add additional context in comments.

10 Comments

What is needed is a {% endwith %}
@Ashley This is the error I'm getting " 'with' expected at least one variable assignment" after making the change
@MaximeK do you mean immediately after the line "{% with i = 0 %}", instead of at the end of for loop?
@Harry do you still get the error if you remove the spaces around the i?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.