簡體   English   中英

如何在Javascript中使用Django for循環?

[英]How to use a Django for-loop in Javascript?

我正在嘗試遍歷Javascript中名為special_ads的Django數組。 我的想法是,我可以創建Javascript 廣告對象並將其存儲在Javascript數組中。 這些對象用於選擇某個廣告並顯示其他信息。

<script type="text/javascript">
  ADS = new slideshow();
  {% for ad in special_ads %}
    ADS.add_ad(new advert(
              "{{ ad.image }}",
              "Drie halen twee betalen",
              "{{ ad.company.name }}",
              "{{ ad.description }}",
              "{{ MEDIA_URL }}{{ ad.image }}",
              "{% thumbnail ad.image 55x55 crop %}",
              "brown",
              "white"
            ));
  {% endfor %}
</script>


//==================================================
// ad object
//==================================================
function advert(id,title,company,description,normal_image_src,thumb_image_src,background_color,text_color) {
  this.id = id;
  this.title = title;
  this.company = company;
  this.description = description;
  this.normal_image_src = normal_image_src;
  this.thumb_image_src = thumb_image_src;
  this.background_color = background_color;
  this.text_color = text_color;
}

我不能真正使用JSON列表,因為在頁面加載時我也需要html中的相同數組,如下所示。

{% for ad in special_ads %}
  <dd>
    <a id="std_ad_{{ i }}" class="img">
      <img id="{{ ad.image }}" class="enlarge" src="{% thumbnail ad.image 55x55 crop %}" alt="{{ ad.company.name }}" onclick="ADS.display(this)"/>
    </a>
  </dd>
{% endfor %}

問題是這不能正常工作。 頁面可以正確加載,但是廣告不會添加到數組中。 而且Django部分似乎可以正確執行。 頁面源結果如下。

<script type="text/javascript">
  ADS = new slideshow();

    ADS.add_ad(new advert(
              "ads/logo_copy.jpg",
              "Drie halen twee betalen",
              "Directdoen.nl",
              "DirectDoen helpt u graag met schoonmaken, tuinonderhoud en klussen. Bij DirectDoen bent u voor hulp in en om uw huis aan",
              "http://127.0.0.1:8000/media/ads/logo_copy.jpg",
              "",
              "brown",
              "white"
            ));

    ADS.add_ad(new advert(
              "ads/Untitled-1.jpg",
              "Drie halen twee betalen",
              "Jouwstraat.nl",
              "Jouwstraat.nl is een website waar buren &amp; straatgenoten met elkaar in contact kunnen
komen en blijven. Kijk dus snel op .
",
              "http://127.0.0.1:8000/media/ads/Untitled-1.jpg",
              "",
              "brown",
              "white"
            ));

    ADS.add_ad(new advert(
              "ads/AD.JPG",
              "Drie halen twee betalen",
              "Code 06",
              "DirectDoen helpt u graag met schoonmaken, tuinonderhoud en klussen. Bij DirectDoen bent u voor hulp in en om uw huis aan
",
              "http://127.0.0.1:8000/media/ads/AD.JPG",
              "http://127.0.0.1:8000/media/ads/AD_JPG_55x55_crop_q95.jpg",
              "brown",
              "white"
            ));

</script>

我已經進行了很多搜索,但是找不到關於如何執行此操作的很好的教程。 有誰知道實現我的目標的最佳方法是什么?

這個問題是不正確的,因為Django for循環顯然不是您的問題。 我想這與advert或slideshow類有關。 您的js控制台不會出現任何錯誤嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM