【问题标题】:How to run python code in html page using django如何使用django在html页面中运行python代码
【发布时间】:2019-09-22 03:07:39
【问题描述】:

我正在学习使用 django 框架进行 Web 开发。我在 html 页面中运行 python 代码没有什么问题。我需要遍历一个字符串列表,该列表基本上包含我想在轮播中显示的图像路径。我收到此错误 “第 83 行的块标记无效:'with.i',预期为 'endblock'。您是否忘记注册或加载此标记?” 在我尝试时在galleryPage.html 文件中打开该页面。这些是我到目前为止写的几行代码

/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 %}

【问题讨论】:

    标签: python django


    【解决方案1】:
    {% with.i = 0 %}
    

    应该是

    {% with i=0 %}
    

    为什么你有 imageList.0 而不是 imageList?

    另外,我认为您可能需要考虑将当前的“with”和“for”组合成一个“for”循环,就像我在上面发布的文档中一样:

    {% 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 %}
    

    这样您就不需要自己增加 i 并且可以消除一些复杂性(以及一些错误来源)。

    【讨论】:

    • 需要的是{% endwith %}
    • @Ashley 这是我在进行更改后得到的错误“'with' expected at least one variable assignment”
    • @MaximeK 你的意思是紧接在“{% with i = 0 %}”行之后,而不是在 for 循环的末尾?
    • @Harry 如果删除 i 周围的空格,你还会收到错误吗?
    猜你喜欢
    • 2022-06-18
    • 2017-06-07
    • 1970-01-01
    • 2020-11-05
    • 2015-08-02
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多