質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

483閲覧

urls.pyでpath指定とsettings.pyのDIRSにて参照先の指定をしているのになぜThe empty path didn’t match any of these.と出るのかわかりません

Takahashi

総合スコア0

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2023/05/07 12:09

実現したいこと

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.と出るのかわかりません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

このurls.pyのpathの最初の引数に指定したパスにアクセスしないと404になります。
例えば、下記なら
http://127.0.0.1:8000/admin/
http://127.0.0.1:8000/helloworldurl/
http://127.0.0.1:8000/helloworldurl2/
ならアクセスできるのですが
http://127.0.0.1:8000/は、urls.pyに
path("/", hogehoge)みたいに/を指定しているものがないため404になっているんじゃないかなと思います。

python

1urlpatterns = [ 2 path('admin/', admin.site.urls), 3 path('helloworldurl/', helloworldfunc), 4 path('helloworldurl2/', HelloWorldClass.as_view()), 5]

投稿2023/05/07 12:41

kzy53

総合スコア32

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kzy53

2023/05/07 13:02

すみません。ちょっと修正です。もしかしたら/も不要かもです両方試してみてもらえればと思います。 path('/', hogehoge) から/を削除した下記の方かもです。 path('', hogehoge)
Takahashi

2023/05/07 13:10

回答ありがとうございます! /の場合に表示させるview.pyの内容を指定するということですね。 なるほどです、すっきりしました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問