Opened 8 years ago

Last modified 8 years ago

#28122 closed Bug

Override static/directory_index.html template will raise TypeError: context must be a dict rather than Context — at Initial Version

Reported by: Natt Piyapramote Owned by: nobody
Component: Core (Other) Version: 1.11
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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>
  1. 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',
            ],
        },
    },
]
  1. 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)

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top