【问题标题】:Block content for passing variables from python to HTML阻止将变量从 python 传递到 HTML 的内容
【发布时间】:2018-12-28 20:25:26
【问题描述】:

我正在尝试将一些数据从我的 python 代码发送到 HTML 脚本。 这是我的python代码。我使用列表来发送数据。

def extractMetaData(request):
pdfDir = "C:/PythonPrograms/pdf/"
if pdfDir == "": pdfDir = os.getcwd() + "\\"
pdf_title = []
pdf_author = []
pdf_creationdate = []
pdf_creator = []
pdf_Keywords = []
pdf_producer = []
for pdf in os.listdir(pdfDir):
    fileExtension = pdf.split(".")[-1]
    if fileExtension == "pdf":
        pdfFilename = pdfDir + pdf 
        pdf_toread = PdfFileReader(open(pdfFilename, "rb"))
        pdf_title.append(pdf_toread.getDocumentInfo().title)
        pdf_author.append(pdf_toread.getDocumentInfo().author)
        pdf_creationdate.append(pdf_toread.getDocumentInfo()['/CreationDate'])
        pdf_creator.append(pdf_toread.getDocumentInfo()['/Creator'])
        pdf_Keywords.append(pdf_toread.getDocumentInfo()['/Keywords'])
        pdf_producer.append(pdf_toread.getDocumentInfo().producer)
return render(request,'personal/extract.html',{'content':[str(pdf_title),str(pdf_author),str(pdf_creationdate), str(pdf_creator),str(pdf_Keywords), str(pdf_producer)]})

在 HTML 中,我使用以下代码。

{% block content %}
{% for c in content%}

<p>{{c}}</p>

{% endfor%}
{% endblock %}  

但这会打印第一个列表的所有项目,然后打印第二个列表的所有项目,依此类推。我希望它打印第一个列表的第一个项目,第二个列表的第一个项目,依此类推。然后从每个列表的第二项开始.. 在 jinja 中如何做到这一点?

【问题讨论】:

  • 我认为你应该使用两个forloop。一个用于 pdf 文件,另一个用于每个项目。
  • 那行不通。它逐个字符地打印。
  • 我在回答中解释得更好。
  • 我的建议是在 Python 中准备数据,然后将有序列表传递给模板。只需在列表上进行迭代并将项目保存在新列表中以处理索引。这样,您将避免模板中的复杂逻辑。
  • @Dos 感谢您的建议。我认为这是唯一的出路

标签: html django python-3.x jinja2


【解决方案1】:

我会这样做:

def extractMetaData(request):
    pdfDir = "C:/PythonPrograms/pdf/"
    if pdfDir == "":
        pdfDir = os.getcwd() + "\\"
        pdf_files = []
        for pdf in os.listdir(pdfDir):
            pdf_file = {}  # Create a dictionnary to store values
            fileExtension = pdf.split(".")[-1]
            if fileExtension == "pdf":
                pdfFilename = pdfDir + pdf 
                pdf_toread = PdfFileReader(open(pdfFilename, "rb"))
                pdf_file['title'] = pdf_toread.getDocumentInfo().title
                pdf_file['author'] = pdf_toread.getDocumentInfo().author
                pdf_file['creationdate'] = pdf_toread.getDocumentInfo()['/CreationDate']
                pdf_file['creator'] = pdf_toread.getDocumentInfo()['/Creator']
                pdf_file['keywords'] = pdf_toread.getDocumentInfo()['/Keywords']
                pdf_file['producer'] = pdf_toread.getDocumentInfo().producer
                pdf_files.append(pdf_file)
    return render(request,'personal/extract.html', {'pdf_files': pdf_files})

然后在模板中:

{% block content %}
{% for pdf in pdf_files %}
    {% for item_name, item in pdf_file.items %}
        <p>{{ item_name }} : {{ item }}</p>
    {% endfor %}
{% endfor%}
<hr>
{% endblock %}

我没有测试它,所以它可能有一些错误..

【讨论】:

  • 非常感谢。做了一些改变,它奏效了。 {% block content %} {% for pdf in pdf_files %}

    {{pdf}}

    {% endfor%} {% endblock %}
猜你喜欢
  • 2017-06-12
  • 2012-06-03
  • 2016-10-25
  • 2021-12-29
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2019-01-06
  • 2017-06-30
相关资源
最近更新 更多