权限错误: [错误 13] 通过上下文变量包含模板时权限被拒绝,硬编码时工作正常



我正在尝试将带有模板名称/路径的变量从视图传递到另一个模板,以便我可以在包含标签中使用该变量。我收到此错误:

File "c:program filespython37libsite-packagesdjangotemplateloadersfilesystem.p
y", line 23, in get_contents
with open(origin.name, encoding=self.engine.file_charset) as fp:
PermissionError: [Errno 13] Permission denied: 'D:\temp\YandexDisk\programming\py\n
hl_web_app\templates'

完整回溯:

Internal Server Error: /game/anaheim-ducks-san-jose-sharks2018-10-03/2018020004/
Traceback (most recent call last):
File "c:program filespython37libsite-packagesdjangocorehandlersexception.py",
line 34, in inner
response = get_response(request)
File "c:program filespython37libsite-packagesdjangocorehandlersbase.py", line
115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "c:program filespython37libsite-packagesdjangocorehandlersbase.py", line
113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:tempYandexDiskprogrammingpynhl_web_appplayersviews.py", line 110, in ga
me_detail
return render(request, 'players/game_detail.html', context)
File "c:program filespython37libsite-packagesdjangoshortcuts.py", line 36, in re
nder
content = loader.render_to_string(template_name, context, request, using=using)
File "c:program filespython37libsite-packagesdjangotemplateloader.py", line 62,
in render_to_string
return template.render(context, request)
File "c:program filespython37libsite-packagesdjangotemplatebackendsdjango.py",
line 61, in render
return self.template.render(context)
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 171,
in render
return self._render(context)
File "c:program filespython37libsite-packagesdjangotestutils.py", line 96, in i
nstrumented_test_render
return self.nodelist.render(context)
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 937,
in render
bit = node.render_annotated(context)
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 904,
in render_annotated
return self.render(context)
File "c:program filespython37libsite-packagesdjangotemplateloader_tags.py", lin
e 150, in render
return compiled_parent._render(context)
File "c:program filespython37libsite-packagesdjangotestutils.py", line 96, in i
nstrumented_test_render
return self.nodelist.render(context)
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 937,
in render
bit = node.render_annotated(context)
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 904,
in render_annotated
return self.render(context)
File "c:program filespython37libsite-packagesdjangotemplateloader_tags.py", lin
e 62, in render
result = block.nodelist.render(context)
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 937,
in render
bit = node.render_annotated(context)
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 904,
in render_annotated
return self.render(context)
File "c:program filespython37libsite-packagesdjangotemplatedefaulttags.py", lin
e 209, in render
nodelist.append(node.render_annotated(context))
File "c:program filespython37libsite-packagesdjangotemplatebase.py", line 904,
in render_annotated
return self.render(context)
File "c:program filespython37libsite-packagesdjangotemplateloader_tags.py", lin
e 176, in render
template = context.template.engine.get_template(template_name)
File "c:program filespython37libsite-packagesdjangotemplateengine.py", line 143
, in get_template
template, origin = self.find_template(template_name)
File "c:program filespython37libsite-packagesdjangotemplateengine.py", line 125
, in find_template
template = loader.get_template(name, skip=skip)
File "c:program filespython37libsite-packagesdjangotemplateloadersbase.py", li
ne 24, in get_template
contents = self.get_contents(origin)
File "c:program filespython37libsite-packagesdjangotemplateloadersfilesystem.p
y", line 23, in get_contents
with open(origin.name, encoding=self.engine.file_charset) as fp:
PermissionError: [Errno 13] Permission denied: 'D:\temp\YandexDisk\programming\py\n
hl_web_app\templates'

视图:

def game_detail(request, slug, nhl_id):
game = Game.objects.get(nhl_id=nhl_id, slug=slug)
user = request.user
sort_order = ['last_name']
away_d_men = utils.filter_position(game.away_skaters.all(), utils.POSITIONS[1], sort_order)
away_fwds = utils.filter_position(game.away_skaters.all(), utils.POSITIONS[2:], sort_order)
home_d_men = utils.filter_position(game.home_skaters.all(), utils.POSITIONS[1], sort_order)
home_fwds = utils.filter_position(game.home_skaters.all(), utils.POSITIONS[2:], sort_order)
template = 'players/game_goalies_table.html'
context = {
'game': game,
'skaters': [
{
'header': game.teams.all()[0],
'list': away_d_men,
'type': utils.DEF,
'table_id': utils.TABLE_IDS[2]
},
{
'list': away_fwds,
'type': utils.FRW,
'table_id': utils.TABLE_IDS[3],
'game_goalies_table': template,
'whitespace': utils.WHITESPACE,
},
{
'header': game.teams.all()[1],
'list': home_d_men,
'type': utils.DEF,
'table_id': utils.TABLE_IDS[4],
},
{
'list': home_fwds,
'type': utils.FRW,
'table_id': utils.TABLE_IDS[5],
'game_goalies_table': template,
'whitespace': utils.WHITESPACE,
},
],
}
if user.is_authenticated:
fav_away_goalies = game.away_goalies.all().filter(favoriting__username=user)
fav_home_goalies = game.home_goalies.all().filter(favoriting__username=user)
fav_away_skaters = game.away_skaters.all().filter(favoriting__username=user)
fav_home_skaters = game.home_skaters.all().filter(favoriting__username=user)
fav_players = list(chain(fav_away_goalies, fav_home_goalies,
fav_away_skaters, fav_home_skaters))
context['favorites'] = fav_players
return render(request, 'players/game_detail.html', context)

主模板:

{% extends 'base.html' %}
{% load static %}
{% load tags %}
{% block styles %}
<link rel="stylesheet" href="{% static 'players/tablesorter.css' %}">
{% endblock styles %}
{% block content %}
{# {% for team in game.teams.all %} #}
{# <img src="{{ team.image.url }}" class="in-div"> #}
{# {% endfor %} #}
<!-- FAVORITES MESSAGE -->
<div id="team-det-fav-alert" class="alert alert-primary js-fav-alert" role="alert">
<span class="js-fav-message"></span>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% for item in skaters %}
<h3>{{ item.header }}</h3>
<h5>{{ item.type }}</h5>
<table id={{ item.table_id }} class="tablesorter">
<thead>
<tr>
<th class="cell-with-tooltip">Fv
<span class="css-tooltip">Follow/Unfollow</span>
</th>
<th class="cell-with-tooltip">#
<span class="css-tooltip">Jersey number</span>
</th>
<th class="sorter-last-name">Name</th>
<th class="cell-with-tooltip">Pos
<span class="css-tooltip">Position</span>
</th>
<th class="cell-with-tooltip">Age
<span class="css-tooltip">Age</span>
</th>
<th class="cell-with-tooltip">G
<span class="css-tooltip">Goals</span>
</th>
<th class="cell-with-tooltip">A
<span class="css-tooltip">Assists</span>
</th>
<th class="cell-with-tooltip">+/-
<span class="css-tooltip">Plus/Minus<span>
</th>
<th class="cell-with-tooltip">PIM
<span class="css-tooltip">Penalty in Minutes</span>
</th>
<th class="cell-with-tooltip">SOG
<span class="css-tooltip">Shots on Goal</span>
</th>
<th class="cell-with-tooltip">Hits
<span class="css-tooltip">Hits</span>
</th>
<th class="cell-with-tooltip">Blk
<span class="css-tooltip">Blocks</span>
</th>
<th class="cell-with-tooltip">FW
<span class="css-tooltip">Faceoff Wins</span>
</th>
<th class="cell-with-tooltip">PPP
<span class="css-tooltip">Power Play Points</span>
</th>
<th class="cell-with-tooltip">SHP
<span class="css-tooltip">Short-Handed Points</span>
</th>
<th class="cell-with-tooltip sorter-countdown">TOI
<span class="css-tooltip">Time on Ice</span>
</th>
<th class="cell-with-tooltip sorter-countdown">TOI PP
<span class="css-tooltip">Time on Ice in Power Play</span>
</th>
<th class="cell-with-tooltip sorter-countdown">TOI SH
<span class="css-tooltip">Time on Ice in Short-Handed</span>
</th>
</tr>
</thead>
<tbody>
{% include 'players/partial_game_detail_tbody.html' %}
</tbody>
</table>
{% include item.game_goalies_table %}
{{ item.whitespace|safe }}
{% endfor %}
{% endblock content %}
{% block scripts %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.1/js/jquery.tablesorter.js"></script>
<!-- Widgets -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.1/js/jquery.tablesorter.widgets.js"></script>
<script src="{% static 'players/last_name.js' %}"></script>
<script src="{% static 'players/parser-duration.js' %}"></script>
<script src="{% static 'players/sorting_game_detail.js' %}"></script>
<script src="{% static 'players/ajax_add_fav.js' %}"></script>
<script src="{% static 'players/ajax_del_fav.js' %}"></script>
{% endblock scripts %}

我试图包含的模板(仅用于测试,我不会在这里制作完整的表格,只是访问上下文变量(:

{% for goalie in game.away_goalies.all %}
{{ goalie }}
{% endfor %}

但是当我对模板名称进行硬编码时,它工作得很好。{% include 'players/game_goalies_table.html' %}而不是{% include item.game_goalies_table %}因此,如果在这种情况下有效,权限似乎没有问题? 还从基本项目文件夹中对其他模板尝试了同样的事情。同样的事情。 在SO上发现了很多以某种方式相关的问题。在我的情况下,无法真正找出错误的原因。

权限错误: [错误 13] 权限被拒绝

尝试从视图写入文件时权限被拒绝

模板在/(给定的 URL(中不存在

PermissionError: [errno 13] 在 Django 中被拒绝了权限

您可以在下面的回答中看到,我想出了使用render_to_string实现最终目标的方法,但我仍然想知道是否有更简单的方法,没有额外的功能。

您的某些skaters没有设置模板,因此在 for 循环中,您得到的{% include '' %}为空字符串。这可能会导致 Django 尝试打开第一个模板目录本身,从而导致您看到此权限错误。

在尝试包含它之前,请检查item.game_goalies_table是否确实存在。

最新更新