2016-10-16 7 views
1

使用してHTMLを返す:ジャンゴFileNotFoundError私は、静的なHTMLページを返しジャンゴでビューを作成していて、次のようにそのビューの私のコードがあるコーデック

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()) 

私はジャンゴを起動するときにdev。私のアプリには、私が期待しているindex.htmlのレンダリングではなく、FileNotFoundError at /myapp/[Errno 2] No such file or directory: 'index.html'と対峙しています。私はindex.htmlが私のviews.pyと同じディレクトリにある実際のファイルであることを知っています。同じディレクトリにdebug.txtを開き、同じ結果を返してデバッグを試みました。同じディレクトリにあるPythonシェルを開いて同じファイルを開こうとすると、問題なく動作します。私は徹底的に困惑しているので、どんな洞察も認められるでしょう。

html = open(os.path.dirname(os.path.realpath(__file__)) + 'index.html', "r") 
+0

あなたは、ファイルへの絶対パスを与える必要があります: –

+0

os.path.abspath( "index.html")を使用して、すでにosをインポートしたとしますか?私が試したので、私は同じエラーが発生します。 –

答えて

0

あなたview.pyファイルと同じディレクトリからファイルを開きたい場合は

は、以下を使用します。
関連する問題