Search code examples
djangodjango-viewsmime-types

Adding content_type to render_to_response in Django's views.py causing 'Server Error (500)'


In django 1.11, in views.py I am using the render_to_response function as follows:

return render_to_response(domainObject.template_path, context_dict, context)

This works fine. Now I am trying to specify the content_type for this response as 'txt/html'. So I switch to

content_type = 'txt/html'
return render_to_response(domainObject.template_path, context_dict, context, content_type)

But with this setup the server returns a

Server Error (500)

Following the documentation at https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render-to-response I think I am providing the variables in the right order...

Here is the full 'def' block for reference:

def myview(request):
        context = RequestContext(request)

                if request.homepage:
                        migrationObject = calltomigration()
                else:
                        integrationObject = Integration.objects.filter(subdomain_slug=request.subdomain).get()
        except ObjectDoesNotExist:
                logger.warning(ObjectDoesNotExist)
                raise Http404

        sectionContent = None
        if not request.homepage:
                sectionContent = getLeafpageSectionContent(referenceObject)

        context_dict = {
                'reference': referenceObject,
                'sectionContent': sectionContent,
                'is_homepage': request.homepage
        }

        #  content_type = 'txt/html'

        return render_to_response(domainObject.template_path, context_dict, context)

Here is the NGINX status:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-01-17 16:34:15 UTC; 40s ago
     Docs: man:nginx(8)
  Process: 14517 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=2)
  Process: 14558 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 14546 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 14562 (nginx)
    Tasks: 2 (limit: 1152)
   CGroup: /system.slice/nginx.service
           ├─14562 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─14564 nginx: worker process

Jan 17 16:34:15 ip-172-31-8-232 systemd[1]: nginx.service: Failed with result 'timeout'.
Jan 17 16:34:15 ip-172-31-8-232 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jan 17 16:34:15 ip-172-31-8-232 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 17 16:34:15 ip-172-31-8-232 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jan 17 16:34:15 ip-172-31-8-232 systemd[1]: Started A high performance web server and a reverse proxy server.
[1]+  Done                    sudo systemctl restart nginx ```

Solution

  • Today I fixed the issue. I found out that in render_to_response, the MIME type has to be specified in the third position (at least in the setup I am working on). Most OS/browser combinations figured out the misformed MIME type, with the exception of Edge on PC. Fixed now!