実現したいこと
templates/hello.htmlを表示させたい
前提
django初心者です。
djangoにてアプリを作成しています。
class-based viewでの実装をしています。urls.pyにてファイル設定を行い、views.pyにてclass HelloWorldClassを定義しました。settings.pyのDIRSに[BASE_DIR/'templates']を追加しました。manage.pyと同階層
(project2/helloworldproject)にtemplatesディレクトリ作成し配下にhello.htmlを作成しpython manage.py runserverを実行しました。
発生している問題・エラーメッセージ
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in helloworldproject.urls, Django tried these URL patterns, in this order:
admin/
helloworldurl/
helloworldurl2/
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
該当のソースコード
【urls.py】
from django.contrib import admin
from django.urls import path
from .views import helloworldfunc, HelloWorldClass
urlpatterns = [
path('admin/', admin.site.urls),
path('helloworldurl/', helloworldfunc),
path('helloworldurl2/', HelloWorldClass.as_view()),
]
【views.py】
from django.http import HttpResponse
from django.views.generic import TemplateView
def helloworldfunc(request) :
responseobject = HttpResponse('<h1>hello world</h1>')
return responseobject
class HelloWorldClass(TemplateView) :
template_name = 'hello.html'
【settings.py】
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR /'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
BASE_DIR = Path(file).resolve().parent.parent
試したこと
djangoのツボとコツがゼッタイにわかる本[第2版]を見ながらアプリ作成しています。スペルミスないかの確認、/helloworldurl/では問題なく表示されましたのでコメントアウトして確認を行いました。
補足情報(FW/ツールのバージョンなど)
Python 3.10.6
Django 3.2
urls.pyでpath指定とsettings.pyのDIRSにてmanage.pyと同階層にtemplateディレクトリ作成し参照先の指定をしているのにThe empty path didn’t match any of these.と出るのかわかりません。
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/05/07 13:02
2023/05/07 13:10