Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28122 closed Bug (fixed)

Overriding directory_index view's static/directory_index.html crashes with TypeError: context must be a dict rather than Context

Reported by: Natt Piyapramote Owned by: Tim Graham
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 (last modified by Natt Piyapramote)

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: /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)

Change History (6)

comment:1 by Natt Piyapramote, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Component: UncategorizedCore (Other)
Owner: changed from nobody to Tim Graham
Severity: NormalRelease blocker
Status: newassigned
Summary: Override static/directory_index.html template will raise TypeError: context must be a dict rather than ContextOverriding directory_index view's static/directory_index.html crashes with TypeError: context must be a dict rather than Context
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

django.views.static.directory_index() must be updated after 6a7495051304d75865add6ff96422018984e1663.

comment:3 by Tim Graham, 7 years ago

Has patch: set

comment:4 by Claude Paroz, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by GitHub <noreply@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 56970c5b:

Fixed #28122 -- Fixed crash when overriding views.static.directory_index()'s template.

comment:6 by Tim Graham <timograham@…>, 7 years ago

In 4a89000c:

[1.11.x] Fixed #28122 -- Fixed crash when overriding views.static.directory_index()'s template.

Backport of 56970c5b61f8f1612944dc54b72ef210d433066f from master

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