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")
あなたは、ファイルへの絶対パスを与える必要があります: –
os.path.abspath( "index.html")を使用して、すでにosをインポートしたとしますか?私が試したので、私は同じエラーが発生します。 –