#28122: Override static/directory_index.html template will raise TypeError:
context
must be a dict rather than Context
----------------------------------+--------------------------------------
Reporter: Natt Piyapramote | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Description changed by Natt Piyapramote:
Old description:
> **Test step**
>
> 1. Create **templates/static/directory_index.html** with following
> content
>
> {{{
> {% load i18n %}
> <body>
> <h1>{% blocktrans %}Index of {{ directory }}{% endblocktrans %}</h1>
> <ul>
> {% if directory != "/" %}
> <li><a href="../">../</a></li>
> {% endif %}
> {% for f in file_list %}
> <li><a href="{{ f|urlencode }}">{{ f }}</a></li>
> {% endfor %}
> </ul>
> </body>
> </html>
> }}}
>
> 2. Config **settings.py**'s TEMPLATE_DIR to search **templates/**
>
> {{{
> TEMPLATES = [
> {
> 'BACKEND': 'django.template.backends.django.DjangoTemplates',
> 'DIRS': [
> 'templates' # add config
> here
> ],
> 'APP_DIRS': True,
> 'OPTIONS': {
> 'context_processors': [
> 'django.template.context_processors.debug',
> 'django.template.context_processors.request',
> 'django.contrib.auth.context_processors.auth',
> 'django.contrib.messages.context_processors.messages',
> ],
> },
> },
> ]
> }}}
>
> 3. Use **django.views.static.serve** in **urls.py**
>
> {{{
> def browse(request):
> return serve(request, request.path, '/tmp', show_indexes=True)
>
> urlpatterns = [
> url(r'^browse/', browse),
> }}}
>
> **Actual result**
> {{{
> TypeError at /browse/
> context must be a dict rather than Context.
> Request Method: GET
> Request URL: http://localhost:8001/browse/
> Django Version: 1.11
> Exception Type: TypeError
> Exception Value:
> context must be a dict rather than Context.
> Exception Location: /home/natt/.virtualenvs/django111/lib/python3.5
> /site-packages/django/template/context.py in make_context, line 287
> Python Executable: /home/natt/.virtualenvs/django111/bin/python
> Python Version: 3.5.2
> Python Path:
> ['/home/natt/tmp/demo',
> '/home/natt/.virtualenvs/django111/lib/python35.zip',
> '/home/natt/.virtualenvs/django111/lib/python3.5',
> '/home/natt/.virtualenvs/django111/lib/python3.5/plat-x86_64-linux-gnu',
> '/home/natt/.virtualenvs/django111/lib/python3.5/lib-dynload',
> '/usr/lib/python3.5',
> '/usr/lib/python3.5/plat-x86_64-linux-gnu',
> '/home/natt/.virtualenvs/django111/lib/python3.5/site-packages']
> Server time: Tue, 25 Apr 2017 07:24:00 +0000
>
> Internal Server Error: /qml/
> Traceback (most recent call last):
> File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
> packages/django/core/handlers/exception.py", line 41, in inner
> response = get_response(request)
> File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
> packages/django/core/handlers/base.py", line 187, in _get_response
> response = self.process_exception_by_middleware(e, request)
> File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
> packages/django/core/handlers/base.py", line 185, in _get_response
> response = wrapped_callback(request, *callback_args,
> **callback_kwargs)
> File "/home/natt/tmp/demo/demo/urls.py", line 23, in main
> return serve(request, path, '/home/natt/cnmi/his/his/qmlweb/qml',
> show_indexes=True)
> File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
> packages/django/views/static.py", line 43, in serve
> return directory_index(path, fullpath)
> File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
> packages/django/views/static.py", line 107, in directory_index
> return HttpResponse(t.render(c))
> File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
> packages/django/template/backends/django.py", line 64, in render
> context = make_context(context, request,
> autoescape=self.backend.engine.autoescape)
> File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
> packages/django/template/context.py", line 287, in make_context
> raise TypeError('context must be a dict rather than %s.' %
> context.__class__.__name__)
> TypeError: context must be a dict rather than Context.
> }}}
>
> **Expected result**
> Directory browsing render successfully
> (This works correctly in Django 1.10)
New description:
**Test step**
1. Create **templates/static/directory_index.html** with following
content
{{{
{% load i18n %}
<body>
<h1>{% blocktrans %}Index of {{ directory }}{% endblocktrans %}</h1>
<ul>
{% if directory != "/" %}
<li><a href="../">../</a></li>
{% endif %}
{% for f in file_list %}
<li><a href="{{ f|urlencode }}">{{ f }}</a></li>
{% endfor %}
</ul>
</body>
</html>
}}}
2. Config **settings.py**'s TEMPLATE_DIR to search **templates/**
{{{
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'templates' # add config
here
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
}}}
3. Use **django.views.static.serve** in **urls.py**
{{{
def browse(request):
return serve(request, request.path, '/tmp', show_indexes=True)
urlpatterns = [
url(r'^browse/', browse),
}}}
**Actual result**
{{{
TypeError at /browse/
context must be a dict rather than Context.
Request Method: GET
Request URL: http://localhost:8001/browse/
Django Version: 1.11
Exception Type: TypeError
Exception Value:
context must be a dict rather than Context.
Exception Location: /home/natt/.virtualenvs/django111/lib/python3.5
/site-packages/django/template/context.py in make_context, line 287
Python Executable: /home/natt/.virtualenvs/django111/bin/python
Python Version: 3.5.2
Python Path:
['/home/natt/tmp/demo',
'/home/natt/.virtualenvs/django111/lib/python35.zip',
'/home/natt/.virtualenvs/django111/lib/python3.5',
'/home/natt/.virtualenvs/django111/lib/python3.5/plat-x86_64-linux-gnu',
'/home/natt/.virtualenvs/django111/lib/python3.5/lib-dynload',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/home/natt/.virtualenvs/django111/lib/python3.5/site-packages']
Server time: Tue, 25 Apr 2017 07:24:00 +0000
Internal Server Error: /browse/
Traceback (most recent call last):
File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/home/natt/tmp/demo/demo/urls.py", line 23, in main
return serve(request, path, '/tmp', show_indexes=True)
File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
packages/django/views/static.py", line 43, in serve
return directory_index(path, fullpath)
File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
packages/django/views/static.py", line 107, in directory_index
return HttpResponse(t.render(c))
File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
packages/django/template/backends/django.py", line 64, in render
context = make_context(context, request,
autoescape=self.backend.engine.autoescape)
File "/home/natt/.virtualenvs/django111/lib/python3.5/site-
packages/django/template/context.py", line 287, in make_context
raise TypeError('context must be a dict rather than %s.' %
context.__class__.__name__)
TypeError: context must be a dict rather than Context.
}}}
**Expected result**
Directory browsing render successfully
(This works correctly in Django 1.10)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28122#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.533ac28623475eed5aa4b28166a4f84e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.