I'm trying to pass a variable with a template name/path from views to the another template, so that I can use the variable in the include tag. I'm getting this error:
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'
Full traceback:
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'
views:
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)
Main template:
{% 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">×</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 %}
The template I'm trying to include(just for test I'm not making a full table here, just accessing context variables):
{% for goalie in game.away_goalies.all %}
{{ goalie }}
{% endfor %}
But it works just fine when I'm hardcoding the template name.
{% include 'players/game_goalies_table.html' %}
instead of {% include item.game_goalies_table %}
So it doesn't seem like there is some problem with permissions if it works in this case?
Also tried the same thing with other templates, from the base project folder. The same thing.
Found a lot of somehow related questions on SO. Couldn't really figure out the cause of the error in my case.
PermissionError: [Errno 13] Permission denied
Permission denied when trying to write to file from a view
template does not exist at /(given url)
PermissionError: [Errno 13] Permission denied in Django
You could see in my answer below that I figured out the way to achieve my end goal using render_to_string
, but I still would like to know if there is simpler way, without extra function.