【问题标题】:How to access multidimensional array using django templates in html page如何在 html 页面中使用 django 模板访问多维数组
【发布时间】:2016-02-25 05:14:44
【问题描述】:

我在 php 页面中有一个多维数组,如下所示:

$comment=Array
(
    [655436] => 
    [655435] => Array
        (
            [id] => 5864928
            [shortmessage] => rytrht
        )

    [655434] => Array
        (
            [id] => 5864934
            [shortmessage] => vcxv
        )

    [654990] => 
    [654989] => Array
        (
            [id] => 5864948
            [shortmessage] => fgfj
        )

    [654983] => Array
        (
            [id] => 5864909
            [shortmessage] => state
        )

    [654981] => 
    [654979] => 
    [654978] => Array
        (
            [id] => 5864933
            [shortmessage] => ggjj
        )

    [654329] => Array
        (
            [id] => 5864974
            [shortmessage] => 
        )

)

echo $renderer->render('sample.html', array( 'index' => commentId,'Comment' => $comments));

我希望使用 Django 模板在 HTML 页面中使用 $comment 数组值而不循环数组

示例.html:

<td><input class="Comment" type="text" value="{{Comment[index]['shormessage']}}" /></td>

但是{{Comment[index]['shormessage']}} 没有给出任何值并且整个页面本身没有显示 我不知道如何访问多维数组。

有什么办法吗?

【问题讨论】:

  • 您的问题有点令人困惑,您在 php 项目中究竟是如何使用 django 的?
  • 在我的 php 页面中我使用了 render() 并且在 html 页面中使用了 django
  • @Nithya,就php吧,不要让你的生活复杂化,php.net/manual/en/function.vsprintf.php

标签: php html django multidimensional-array


【解决方案1】:

为此使用 For 循环 尝试使用

{% for Comment in Comments %}

<input class="Comment" type="text" value="{{Comment.shormessage}}" />

{%endfor%}

【讨论】:

    猜你喜欢
    • 2017-11-24
    • 2012-10-03
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2015-05-27
    • 1970-01-01
    相关资源
    最近更新 更多