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)
--
Ticket URL: <https://code.djangoproject.com/ticket/28122>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
New description:
**Test step**
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>
* status: new => assigned
* severity: Normal => Release blocker
* component: Uncategorized => Core (Other)
* owner: nobody => Tim Graham
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted
Comment:
`django.views.static.directory_index()` must be updated after
6a7495051304d75865add6ff96422018984e1663.
--
Ticket URL: <https://code.djangoproject.com/ticket/28122#comment:2>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/8403 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/28122#comment:3>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/28122#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"56970c5b61f8f1612944dc54b72ef210d433066f" 56970c5b]:
{{{
#!CommitTicketReference repository=""
revision="56970c5b61f8f1612944dc54b72ef210d433066f"
Fixed #28122 -- Fixed crash when overriding
views.static.directory_index()'s template.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28122#comment:5>
Comment (by Tim Graham <timograham@…>):
In [changeset:"4a89000ccf0483c67f66709cca90fc50ad82aacc" 4a89000c]:
{{{
#!CommitTicketReference repository=""
revision="4a89000ccf0483c67f66709cca90fc50ad82aacc"
[1.11.x] Fixed #28122 -- Fixed crash when overriding
views.static.directory_index()'s template.
Backport of 56970c5b61f8f1612944dc54b72ef210d433066f from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28122#comment:6>