Python Django:使用模板中视图中的变量

Python Django:使用模板中视图中的变量,python,django,Python,Django,我想在django的模板中使用名为“files”的变量的内容。My views.py如下所示: from django.shortcuts import render import os def index(request): os.chdir("/home/ubuntu/newproject/static") for files in os.listdir("."): return render(request, 'sslc

我想在django的模板中使用名为“files”的变量的内容。My views.py如下所示:

from django.shortcuts import render

import os


def index(request):
        os.chdir("/home/ubuntu/newproject/static")
        for files in os.listdir("."):
                return render(request, 'sslcert/index.html','files')
<head>
        {% block title %}
        <h3>
                        Following directories are in this folder:
        </h3>
        {% endblock %}
</head>



<body>
        <<(HERE SHOULD BE THE OUTCOME OF THE VARIABLE LIST)>>
</body>
from django.shortcuts import render_to_response

def index(request):
    os.chdir("/home/ubuntu/newproject/static")
    for file in os.listdir("."):
        files.append(file)
    return render_to_response('sslcert/index.html', {'files':files})
我的名为“index.html”的模板如下所示:

from django.shortcuts import render

import os


def index(request):
        os.chdir("/home/ubuntu/newproject/static")
        for files in os.listdir("."):
                return render(request, 'sslcert/index.html','files')
<head>
        {% block title %}
        <h3>
                        Following directories are in this folder:
        </h3>
        {% endblock %}
</head>



<body>
        <<(HERE SHOULD BE THE OUTCOME OF THE VARIABLE LIST)>>
</body>
from django.shortcuts import render_to_response

def index(request):
    os.chdir("/home/ubuntu/newproject/static")
    for file in os.listdir("."):
        files.append(file)
    return render_to_response('sslcert/index.html', {'files':files})

{%block title%}
此文件夹中有以下目录:
{%endblock%}
帮助和解释也很酷:/我是django的真正初学者,我想知道这个模板和视图是如何连接的:)如果这个问题真的很愚蠢,请不要讨厌我:(

做一些类似的事情:

from django.shortcuts import render
import os

def index(request):
        os.chdir("/home/ubuntu/newproject/static")
        files = []
        for file in os.listdir("."):
            files.append(file)

        context = {'files':files}
        return render(request, 'sslcert/index.html', context)
然后是模板:

<head>
        {% block title %}
        <h3>
              Following directories are in this folder:
        </h3>
        {% endblock %}
</head>

<body>
       {{ files }}
</body>

{%block title%}
此文件夹中有以下目录:
{%endblock%}
{{files}}
执行以下操作:

from django.shortcuts import render
import os

def index(request):
        os.chdir("/home/ubuntu/newproject/static")
        files = []
        for file in os.listdir("."):
            files.append(file)

        context = {'files':files}
        return render(request, 'sslcert/index.html', context)
然后是模板:

<head>
        {% block title %}
        <h3>
              Following directories are in this folder:
        </h3>
        {% endblock %}
</head>

<body>
       {{ files }}
</body>

{%block title%}
此文件夹中有以下目录:
{%endblock%}
{{files}}

您可以像这样将变量传递给模板:

from django.shortcuts import render

import os


def index(request):
        os.chdir("/home/ubuntu/newproject/static")
        for files in os.listdir("."):
                return render(request, 'sslcert/index.html','files')
<head>
        {% block title %}
        <h3>
                        Following directories are in this folder:
        </h3>
        {% endblock %}
</head>



<body>
        <<(HERE SHOULD BE THE OUTCOME OF THE VARIABLE LIST)>>
</body>
from django.shortcuts import render_to_response

def index(request):
    os.chdir("/home/ubuntu/newproject/static")
    for file in os.listdir("."):
        files.append(file)
    return render_to_response('sslcert/index.html', {'files':files})
在模板中,您可以像这样使用它:

{{files}}
如果您想使用整个字段,或者可以循环使用它们

{% for file in files %}
# do something with file here
{% endfor %}

您可以按如下方式将变量传递给模板:

from django.shortcuts import render

import os


def index(request):
        os.chdir("/home/ubuntu/newproject/static")
        for files in os.listdir("."):
                return render(request, 'sslcert/index.html','files')
<head>
        {% block title %}
        <h3>
                        Following directories are in this folder:
        </h3>
        {% endblock %}
</head>



<body>
        <<(HERE SHOULD BE THE OUTCOME OF THE VARIABLE LIST)>>
</body>
from django.shortcuts import render_to_response

def index(request):
    os.chdir("/home/ubuntu/newproject/static")
    for file in os.listdir("."):
        files.append(file)
    return render_to_response('sslcert/index.html', {'files':files})
在模板中,您可以像这样使用它:

{{files}}
如果您想使用整个字段,或者可以循环使用它们

{% for file in files %}
# do something with file here
{% endfor %}

您在示例中使用的呈现函数得到了dictionary参数,该参数可以扩展传递到模板中的上下文

呈现(请求、模板名称[、字典][、上下文实例][、内容类型][、状态][、当前应用程序][、目录])

字典 要添加到模板上下文的值字典。默认情况下,这是一个空字典。如果字典中的值可调用,视图将在呈现模板之前调用它

所以,您可以将任何数据作为字典传递到模板中,这些键将作为变量在模板中可用

from django.shortcuts import render

def index(request):
    dir = "/home/ubuntu/newproject/static"
    return render('sslcert/index.html', {'files': os.listdir(dir)})

您在示例中使用的呈现函数得到了dictionary参数,该参数可以扩展传递到模板中的上下文

呈现(请求、模板名称[、字典][、上下文实例][、内容类型][、状态][、当前应用程序][、目录])

字典 要添加到模板上下文的值字典。默认情况下,这是一个空字典。如果字典中的值可调用,视图将在呈现模板之前调用它

所以,您可以将任何数据作为字典传递到模板中,这些键将作为变量在模板中可用

from django.shortcuts import render

def index(request):
    dir = "/home/ubuntu/newproject/static"
    return render('sslcert/index.html', {'files': os.listdir(dir)})

你正在尝试循环渲染模板。只需将你的
文件
传递到
渲染
,而不是在其上循环。哦,该死……我可能对这个问题投了反对票,但“将文件传递到渲染”是什么意思?您正在尝试循环呈现模板。只需将您的
文件
传递到
呈现
,而不是在其上循环。哦,该死……我可能对这个问题投了反对票,但“将文件传递到呈现”是什么意思?谢谢您的回答:)但是出现了一个类型错误:“呈现到字符串()获取了一个意外的关键字参数“files”,即“不知道为什么”:,因为这不是向模板传递上下文的方式。你应该传一本字典:
{'files':files}
.aaaaaaaaaaaaaaaaaayyyyyyyy你是爸爸:成功了:))谢谢你,伙计!顺便问一下,你能告诉我你在哪里学的这些东西吗?或者你能给我推荐一些好的教程吗?谢谢:)@ZedsWhatSheSaid:所有这些都有文档记录:对
os.chgdir
的调用是无用的,您也可以在所需目录上调用
os.listdir()。另外,
os.listdir
返回一个列表,因此在该列表上进行迭代以填充另一个列表纯粹是浪费时间和资源……感谢您的回答:)但是出现了一个类型错误:“render_to_string()得到了一个意外的关键字参数‘files’”不知道为什么:/。你应该传一本字典:
{'files':files}
.aaaaaaaaaaaaaaaaaayyyyyyyy你是爸爸:成功了:))谢谢你,伙计!顺便问一下,你能告诉我你在哪里学的这些东西吗?或者你能给我推荐一些好的教程吗?谢谢:)@ZedsWhatSheSaid:所有这些都有文档记录:对
os.chgdir
的调用是无用的,您也可以在所需目录上调用
os.listdir()。另外,
os.listdir
返回一个列表,因此在其上迭代以填充另一个列表纯粹是浪费时间和资源……我感谢您的回答:)但这给了我一个语法错误。它告诉我,“return render\u to\u response('sslcert/index.html','files':files')这一行中有一个无效语法。我希望将模板的命令放在正确的位置,我将它们放在body标记之间,现在看起来是这样的:{%for file in files%}{%endfor%}Edit:另外,我还编辑了新的导入;仍然不起作用:/that one没有给我一个语法错误,但它仍然不起作用:/it只给我这一行“下面的目录在这个文件夹中:”然后没有任何后续内容,但仍然感谢您的帮助:)我感谢您的回答:)但这给了我一个语法错误。它告诉我,“return render\u to\u response('sslcert/index.html','files':files')这一行中有一个无效语法。我希望将模板的命令放在正确的位置,我将它们放在body标记之间,现在看起来是这样的:{%for file in files%}{%endfor%}Edit:另外,我还编辑了新的导入;仍然不起作用:/that one没有给我一个语法错误,但仍然不起作用:/it只给我这一行“以下目录在此文件夹中:”然后没有任何后续内容,但仍然感谢您的帮助:)