语法“ 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的%%”行的错误消息。这是完整的错误消息:
` * / inforeflex_gener_html / cellules处的TypeError
“方法”对象不可迭代
请求方法:GET 要求网址: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