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

Python PermissionError:[Errno 13]通过上下文变量包含模板时权限被拒绝,硬编码时可以正常工作,python,django,python-3.x,windows,django-templates,Python,Django,Python 3.x,Windows,Django Templates,我试图将一个具有模板名称/路径的变量从视图传递到另一个模板,以便在标记中使用该变量。我得到了这个错误: File "c:\program files\python37\lib\site-packages\django\template\loaders\filesystem.p y", line 23, in get_contents with open(origin.name, encoding=self.engine.file_charset) as fp: P

我试图将一个具有模板名称/路径的变量从视图传递到另一个模板,以便在标记中使用该变量。我得到了这个错误:

  File "c:\program files\python37\lib\site-packages\django\template\loaders\filesystem.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 files\python37\lib\site-packages\django\core\handlers\exception.py",
line 34, in inner
    response = get_response(request)
  File "c:\program files\python37\lib\site-packages\django\core\handlers\base.py", line
115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "c:\program files\python37\lib\site-packages\django\core\handlers\base.py", line
113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\temp\YandexDisk\programming\py\nhl_web_app\players\views.py", line 110, in ga
me_detail
    return render(request, 'players/game_detail.html', context)
  File "c:\program files\python37\lib\site-packages\django\shortcuts.py", line 36, in re
nder
    content = loader.render_to_string(template_name, context, request, using=using)
  File "c:\program files\python37\lib\site-packages\django\template\loader.py", line 62,
 in render_to_string
    return template.render(context, request)
  File "c:\program files\python37\lib\site-packages\django\template\backends\django.py",
 line 61, in render
    return self.template.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 171,
in render
    return self._render(context)
  File "c:\program files\python37\lib\site-packages\django\test\utils.py", line 96, in i
nstrumented_test_render
    return self.nodelist.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 937,
in render
    bit = node.render_annotated(context)
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 904,
in render_annotated
    return self.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\loader_tags.py", lin
e 150, in render
    return compiled_parent._render(context)
  File "c:\program files\python37\lib\site-packages\django\test\utils.py", line 96, in i
nstrumented_test_render
    return self.nodelist.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 937,
in render
    bit = node.render_annotated(context)
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 904,
in render_annotated
    return self.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\loader_tags.py", lin
e 62, in render
    result = block.nodelist.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 937,
in render
    bit = node.render_annotated(context)
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 904,
in render_annotated
    return self.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\defaulttags.py", lin
e 209, in render
    nodelist.append(node.render_annotated(context))
  File "c:\program files\python37\lib\site-packages\django\template\base.py", line 904,
in render_annotated
    return self.render(context)
  File "c:\program files\python37\lib\site-packages\django\template\loader_tags.py", lin
e 176, in render
    template = context.template.engine.get_template(template_name)
  File "c:\program files\python37\lib\site-packages\django\template\engine.py", line 143
, in get_template
    template, origin = self.find_template(template_name)
  File "c:\program files\python37\lib\site-packages\django\template\engine.py", line 125
, in find_template
    template = loader.get_template(name, skip=skip)
  File "c:\program files\python37\lib\site-packages\django\template\loaders\base.py", li
ne 24, in get_template
    contents = self.get_contents(origin)
  File "c:\program files\python37\lib\site-packages\django\template\loaders\filesystem.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 %}
但是当我硬编码模板名称时,它工作得很好。
{%include'players/game\u goalies\u table.html%}
而不是
{%include item.game\u goalies\u table%}
因此,如果在这种情况下有效,权限似乎没有问题? 还尝试了从基本项目文件夹使用其他模板的相同方法。同样的事情。 发现了很多与此相关的问题。在我的案例中,无法真正找出错误的原因


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

你的一些
滑板手
没有模板集,所以在for循环中,你得到了
{%include'%}
中包含一个空字符串。这可能会导致Django尝试打开第一个模板目录本身,从而导致您看到的这个权限错误


在尝试包含它之前,请检查
item.game\u goalies\u table
是否确实存在。

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


在尝试将其包括在内之前,请检查
item.game\u goalies\u table
是否确实存在。

@Mohamad Shahrestani。你可以同时发布一个答案和一个问题,这是不被禁止的。尽管如此,我还是要验证另一个答案,如果它能帮助我解决这个问题,而不使用
render\u to\u string
你在滑冰者中循环,但是有些人没有
game\u goalies\u table
属性集,在这种情况下,
{%include%}
语句尝试不包含任何内容(并导致尝试包含模板目录本身)。您应该首先检查
项。游戏守门员表
是否已设置。@dirkgroten,好吧,这很好!您可以将此作为答案发布。@Mohamad Shahrestani。您可以同时发布答案和问题,这是不被禁止的。不过,如果有助于我解决问题,我会验证另一个答案使用
render_to_string
你在溜冰者中循环,但是有些人没有设置
game_goalies_table
属性,在这种情况下,
{%include%}
语句尝试不包含任何内容(并导致尝试包含模板目录本身)。您应该首先检查
项。游戏守门员表
是否已设置。@dirkgroten,好吧,这很好!您可以将此作为答案发布。
{% for goalie in game.away_goalies.all %}
    {{ goalie }}
{% endfor %}