Django FileNotFoundError in a view when returning HTML using codecs
<p>I am creating a view in Django that returns a static HTML page, and my code for that view is as follows:</p>

<pre><code>from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render_to_response
import codecs

# Basic HTTP response that returns my html index
# To call this view, map it to a URLconf

def index(request):

    # When a request is called, return my index.html

    html = codecs.open("index.html", "r")
    return HttpResponse(html.read())
</code></pre>

<p>When I fire up the Django dev. server and navigate to my app, instead of the rendering of <code>index.html</code> that I expect, I am confronted with a <code>FileNotFoundError at /myapp/</code> as well as <code>[Errno 2] No such file or directory: 'index.html'</code>. I know for a fact that <code>index.html</code> is a real file in the same directory as my<code>views.py</code>, and I have also tried debugging by opening a <code>debug.txt</code> in the same directory and returning that, but with the same result. When I open the python shell in the same directory and try to open the same file, it works without a hitch. Any insight would be appreciated, as I am thouroughly stumped.</p>

<p>If you want to open a file from the same directory as your view.py file, use the following:</p>

<pre><code>html = open(os.path.dirname(os.path.realpath(__file__)) + 'index.html', "r")
</code></pre>

python html django http web-deployment