・ページにアクセスしたときに、「Non-ASCII character '\xe3' in file views.py on line 9, but no encoding declared;」というエラーが発生し、アクセスできない
原因
・日本語をファイル内で利用したが、文字コードを宣言しなかった
対処法
・日本語を利用したファイルの1行目に次のエンコーディング宣言を追加
# encoding: UTF-8
# encoding: UTF-8
'django.middleware.csrf.CsrfResponseMiddleware',
# 追加するディレクトリ:/home/project/templates TEMPLATE_DIRS = ( '/home/project/templates', )
# 追加するアプリケーションが'/home/project/sampleapp'の場合、
# '/home/project/sampleapp/views.py'に次を追加
from django.http import HttpResponse
from django.template import Context, loader
def index(request):
template = loader.get_template('sampleapp/index.html')
return HttpResponse(template.render(Context()))
urlpatterns = ('',
url(r'^sampleapp/$', 'project.sampleapp.views.index'),
)
print __file__
import os parent_dir = os.path.dirname(__file__)
from os current_path = os.getcwd() print current_path