1

I have a TemplateView that returns a TemplateResponse object in a process_template_response middle-ware, but the later is never triggered.

When i change the middleware method in process_response and preform render() on the TemplateResponse, the middle-ware method is triggered and the page is render.

Which steps do i need preform to render a view in a process_template_response?

View:

class PageView(TemplateView):

    template_name = 'flatpages/default.html'

    def get(self, request, *args, **kwargs):
            url = kwargs['url']
            if not url.endswith('/') and settings.APPEND_SLASH:
                    return HttpResponseRedirect(url + '/')
            if not url.startswith('/'):
                    url = url + '/'
                    kwargs.update({'url': url})
            context = self.get_context_data(**kwargs)
            return self.render_to_response(context)

    def get_context_data(self, **kwargs):
            url = kwargs.pop('url')
            context = super(PageView, self).get_context_data(**kwargs)
            page = get_object_or_404(ParentPage, url__exact=url, sites__id__exact=settings.SITE_ID)
            context.update({'flatpage': page})
            return context

class PageFallbackMiddleware(object):
    def process_template_response(self, request, response):
            print 'Triggered'
            if response.status_code != 404:
                    return response
            try:
                    return PageView.as_view()(request, url=request.path_info)
            except Http404:
                    return response
            except:
                    if settings.DEBUG:
                            raise
                    return response
Lef Ford
  • 11
  • 2

1 Answers1

0

I think you may have to return a TemplateResponse or similar for the middleware to route the view to another template, returning the response parameter or another HTTPResponse instance is a no-op.

Drew Nichols
  • 735
  • 1
  • 6
  • 13