python - 语法“cellule.utilisateurs.all”在 Django HTML 模板中有效,但在 Django TeX 模板中无效
问题描述
语法“cellule.utilisateurs.all”确实适用于 HTML Django 模板,但不适用于 TeX Django 模板。
使用 HTML Django 模板,它会在 WEB 浏览器中显示的结果 HTML 文件中显示每个单元格(法语中的“cellule”)的用户列表(法语中的“utilisateurs”)。
使用 TeX Django 模板,我在 WEB 浏览器中收到以下错误消息:
“方法”对象不可迭代
以下是不同的代码:
HTML 模板:
<html charset="UTF-8">
<body>
<p style="font-size:40px;color:blue">INFOREFLEX_GENER_HTML_CELLULES, version 1.0 (23/07/2019)</p>
<p style="font-size:30px;color:red">(Date de l'import des données en base : {{ dateDernierExport }})</p>
{% for cellule in listeCellules %}
<a id="{{ cellule.dpt_im }}"></a>
<h1 style="font-size:60px;color:blue">{{ cellule.dpt_im }}</h1>
<table border="1">
{% for utilisateur in cellule.utilisateurs.all %}
<tr>
<td style="background-color:moccasin;font-weight: bold"><a href="http://annuaire-entreprise.inetpsa.com/?action=exec_recherche_personne&identifiant={{ utilisateur.cod_user }}" target="_blank">{{ utilisateur.cod_user }}</a>-{{ utilisateur.lib_prenom }} {{ utilisateur.lib_nom_usuel }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
</body>
</html>
TeX 模板:
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{color, colortbl}
\usepackage{hyperref}
\usepackage[margin=0.25in]{geometry}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=cyan,
}
\title{INFOREFLEX_GENER_HTML_CELLULES, version 1.0 (23/07/2019)}
\author{OPEN Emmanuel Duvshani}
\date{\today}
\begin{document}
% \maketitle
\bigskip
(Date de l'import des données en base :\VAR{dateDernierExport})
\bigskip
\newcolumntype{y}{>{\columncolor{yellow}}l}
%% for cellule in listeCellules
\hypertarget{\VAR{cellule.dpt_im}}{\VAR{cellule.dpt_im }}
\bigskip
\begin{flushleft}
\begin{tabular}{ | y | }
\hline
%% for utilisateur in cellule.utilisateurs.all
\hyperlink{http://annuaire-entreprise.inetpsa.com/?action=exec_recherche_personne&identifiant=\VAR{utilisateur.cod_user}}{\VAR{utilisateur.cod_user}}-\VAR{utilisateur.lib_prenom}\VAR{utilisateur.lib_nom_usuel}
\hline
%% endfor
\end{tabular}
\end{flushleft}
\bigskip
%% endfor
\end{document}
函数在“views.py”文件中运行
使用 HTML 模板时:
enter code here
def index_cellules(request):
# Date de dernier export de la base Reflex
listeDateExports = DateExports.objects.all()
dateDernierExport = listeDateExports[0].date[:10]
# Liste des applications de la table Perimetre
listeApplications = Perimetre.objects.values_list('hdkqtap').all()
# Liste des cellules correspondant aux applications de la table Perimetre
listeCellules = Hdkqtif.objects.filter(hdkqtap__in=listeApplications).distinct().order_by('dpt_im')
template = loader.get_template('inforeflex_gener_html/index_cellules.html')
contexte = {
"dateDernierExport" : dateDernierExport,
"listeCellules" : listeCellules,
}
return HttpResponse(template.render(contexte))
使用 TeX 模板(使用 jinja)时:
def index_cellules(request):
# Date de dernier export de la base Reflex
listeDateExports = DateExports.objects.all()
dateDernierExport = listeDateExports[0].date[:10]
# Liste des applications de la table Perimetre
listeApplications = Perimetre.objects.values_list('hdkqtap').all()
# Liste des cellules correspondant aux applications de la table Perimetre
listeCellules = Hdkqtif.objects.filter(hdkqtap__in=listeApplications).distinct().order_by('dpt_im')
latex_jinja_env = jinja2.Environment(
block_start_string = '\BLOCK{',
block_end_string = '}',
variable_start_string = '\VAR{',
variable_end_string = '}',
comment_start_string = '\#{',
comment_end_string = '}',
line_statement_prefix = '%%',
line_comment_prefix = '%#',
trim_blocks = True,
autoescape = False,
loader = jinja2.FileSystemLoader('C:\\EMMANUEL\\inforeflex\\django\\inforeflex\\inforeflex\\templates\\inforeflex_gener_html')
)
template = latex_jinja_env.get_template('index_cellules.tex')
contexte = {
"dateDernierExport" : dateDernierExport,
"listeCellules" : listeCellules,
}
return template.render(dateDernierExport=dateDernierExport, listeCellules=listeCellules)
对于每个单元格,预期结果是格式化的用户列表。实际结果是有关“cellule.utilisateurs.all 中 utilisateur 的 %%”行的错误消息。这是完整的错误信息:
` *TypeError 在 /inforeflex_gener_html/cellules
“方法”对象不可迭代
请求方法:GET 请求 URL: http://localhost:8000/inforeflex_gener_html/cellules Django 版本:2.2.2 异常类型:TypeError 异常值:
“方法”对象不可迭代
异常位置:C:\EMMANUEL\inforeflex\django\inforeflex\inforeflex\templates\inforeflex_gener_html\index_cellules.tex 在顶级模板代码,第 42 行 Python 可执行文件:C:\Users\edu04718\AppData\Local\Programs\Python\ Python37-32\python.exe Python 版本:3.7.3 Python 路径:
['C:\EMMANUEL\inforeflex\django\inforeflex', 'C:\Users\edu04718\AppData\Local\Programs\Python\Python37-32\python37.zip', 'C:\Users\edu04718\AppData\Local \Programs\Python\Python37-32\DLLs', 'C:\Users\edu04718\AppData\Local\Programs\Python\Python37-32\lib', 'C:\Users\edu04718\AppData\Local\Programs\Python \Python37-32', 'C:\Users\edu04718\AppData\Local\Programs\Python\Python37-32\lib\site-packages']
服务器时间:2019 年 9 月 19 日星期四 15:19:47 +0000* `
备注:第 42 行是 TeX 模板中的以下行:
%% for utilisateur in cellule.utilisateurs.all
解决方案
推荐阅读
- java - 在 catch 块中调用 void 方法并引发异常的方法的单元测试
- gcc - 在构建内核模块时设置包含路径
- php - 如何从我的 php 数组中检索一个值?
- python - 如何评估字典中的值?
- python - Python 的 DRF,字段名称“”对模型无效
- sql - 在下面的查询中, ACCOUNT_CHECKER_EXIT 日期时间为空
- python-3.x - TypeError:在创建数据生成器 PyTorch 时,“元组”对象不能被解释为整数
- seaborn - How can I decrease the width of a boxplot rectangle?
- python - OCR Tesseract - 获取图像字体属性
- angular - Angular 7 直接 url 路由不适用于 id