使用Django 1.8.1中的视图列表作为javascript模板中的数组

使用Django 1.8.1中的视图列表作为javascript模板中的数组,javascript,python,arrays,django,templates,Javascript,Python,Arrays,Django,Templates,我在views.py中有一段代码,它从目录中获取文件名: def imgArray(request): filepath = STATIC_PATH+"\\images" imageArray=[] ext='.jpg' for i in os.listdir(filepath): if(os.path.splitext(i)[1] == ext): imageArray.append( i ) context = {'imageArray': imageA

我在views.py中有一段代码,它从目录中获取文件名:

def imgArray(request):
filepath = STATIC_PATH+"\\images"
imageArray=[]
ext='.jpg'
for i in os.listdir(filepath):
    if(os.path.splitext(i)[1] == ext):      
         imageArray.append( i )
context = {'imageArray': imageArray}
print context
return render(request, 'imgpage/add_category.html',context)



def add_category(request):
    # A HTTP POST?
    if request.method == 'POST':
       #Do some actions
    else:
        # If the request was not a POST, display the form to enter details.
        imageArray = imgArray(request)
    return render(request, 'imgpage/add_category.html')
我想在javascript中使用这个图像文件数组。我想使用文件名数组,这样我就可以使用js来更改图像源。
print context
语句在python控制台中产生以下输出:

{'imageArray': ['image0.jpg', 'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.
jpg', 'image5.jpg', 'image6.jpg', 'image7.jpg', 'image8.jpg', 'image9.jpg']}
但我根本无法访问模板中的imageArray。以下是我试图在模板html文件中测试数组是否已通过的脚本:

{% for img in imageArray %}
        <li>{{ img|safe }}</li>
        <li>{{ img }}</li>
        <p>img</p>
{% endfor %}
在add_category.html文件中:

{% for img in imageArray %}
        <li>{{ img|safe }}</li>
        <li>{{ img }}</li>
        <p>img</p>
{% endfor %}
{%用于imageArray%中的img}
  • {{img | safe}}
  • {{img}}
  • img

    {%endfor%}
    另外,请注意,
    标记中的“img”也不会显示在屏幕上

    <script type="text/javascript">
            var images = "{{imageArray|safe}}";
            console.log(images);
            console.log("imgx="+images[1]);
            document.getElementsByTagName("img")[0].src =  DJANGO_STATIC_URL+images[2];
        </script>
    
    
    var images=“{imageArray | safe}}”;
    控制台日志(图像);
    console.log(“imgx=“+images[1]);
    document.getElementsByTagName(“img”)[0].src=DJANGO_STATIC_URL+images[2];
    
    在上面的脚本中,第一个控制台日志在控制台中打印空行,而第二个日志打印“imgx=undefined”


    请建议我如何使用python列表作为JS数组。我使用Django 1.8.1,但我将承载的服务使用1.7.x。因此,对这两个方面都起作用的东西会很好。

    这里有一个非常特殊的结构
    imageArray()
    是一个视图,返回完整的HttpResponse;但您可以从另一个视图中调用它,
    add\u category
    。更重要的是,你对结果一无所知;它被扔掉了,再也没有经过任何地方。所以,很自然,它在模板中总是空白的

    我不确定你到底在做什么,所以很难知道你到底想要什么。但是我怀疑
    imageArray()
    应该是一个正常的实用方法,它只返回一个图像列表:

    def imgArray():
        filepath = os.path.join(STATIC_PATH, "\\images")
        images =[f for f in os.listdir(filepath) if f.endswith('.jpg')]
        return images
    
    然后,您需要在
    add_category
    函数中实际使用该值执行一些操作:

    def add_category(request):
        ...
        else:
            imageArray = imgArray()
        return render(request, 'imgpage/add_category.html', {imageArray: imageArray})
    
    以下是我所做的: views.py

    在add_category.html中:

    <script type="text/javascript">
                var images = {{imageArray|safe}};               
                document.getElementsByTagName("img")[0].src =  DJANGO_STATIC_URL+"images/"+images[1];
            </script>
    
    
    var images={{imageArray | safe}};
    document.getElementsByTagName(“img”)[0].src=DJANGO_STATIC_URL+“images/”+images[1];
    
    希望这对某人有帮助:)
    干杯

    感谢您的快速回复!无论如何,我修改了views.py如下:
    else:imageArray=imgArray(请求)context={'imageArray':imageArray}返回render(请求'imgpage/add_category.html',context)
    其中imageArray具有imgArray函数返回的列表。现在控制台打印以下内容:
    ['image0.jpg'、'image1.jpg'、'image2.jpg'、'image3.jpg'、'image4.jpg'、'image5.jpg'、'image6.jpg'、'image7.jpg'、'image8.jpg'、'image9.jpg']imgx=
    如何将imgx作为文件名?您已经在JS代码中用引号括住了Django变量。因此JS将它视为一个字符串。实际上,您应该使用JSON在Django和JS之间传递值。非常感谢!在使用JSON方面做得很好。