[Answer]-Cannot locate view in Django MVC


The problem is with return HttpResponse(template). The HttpResponse accepts content for it’s first argument. The content could be anything that has a string representation (implements either __str__ or __unicode__, depending on passing Content-Encoding header to the response and it’s charset) or is iterable. In the latter case, all elements yield by it have to representable by a string.

The issue you are experiencing is that you are passing a Template object to the response, which is iterable (implements __iter__ method). Iterating the template yields all the compiled nodes. The nodes themselves implement __repr__ which is used when __str__ is missing. And the HttpResponse, if content is iterable, returns all elements that are yield from iterating the iterable.

You can simulate this:

from django.http import HttpResponse
from django.template import RequestContext, loader

def index(request):
    template = loader.get_template('homepage/index.html')
    for n in template:
        print n
    return HttpResponse(template)

Leave a comment