empty path breaks after adding the first app in django
Question:
I created a new django app and ran it using
python manage.py runserver
. It ran normal and I could see the default page
With the shell outputting:
Django version 4.0.1, using settings 'storefront.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[23/Jan/2022 18:52:07] ←[m"GET / HTTP/1.1" 200 10697←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/css/fonts.css HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0←[0m
Not Found: /favicon.ico
[23/Jan/2022 18:52:07,704] - Broken pipe from ('127.0.0.1', 56807)
Then I added a new app, like so:
python manage.py startapp playground
then I added an action handler
playground/views.py
def say_hello(request):
return HttpResponse('Hello World!')
I also added urls.py
inside the playground
folder
playground/urls.py
urlpatterns = [
path('hello/', views.say_hello)
]
And the main urls.py
I added:
root/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('playground/', include('playground.urls')),
]
In the main settings.py, I included playground
in the INSTALLED_APPS
list.
Then I ran the application, and I can confirm that the path 127.0.0.1:800/playground/hello
works as expected.
However, the empty path 127.0.0.1:8000/
is no longer valid. I get this error:
The empty path only works if I comment the playground
urls,
urlpatterns = [
path('admin/', admin.site.urls),
# once the next line is commented, the empty path works fine
# path('playground/', include('playground.urls')),
]
Here is what the log displays once the empty path is requested:
Django version 4.0.1, using settings 'storefront.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[23/Jan/2022 18:47:36] ←[33m"GET / HTTP/1.1" 404 2174←[0m
Not Found: /favicon.ico
[23/Jan/2022 18:47:36,801] - Broken pipe from ('127.0.0.1', 56786)
What am I doing wrong?
Answers:
You didn’t do anything wrong, the default django page only shows if you don’t have any other url patterns to try (Github)
Now you need to add a path at /
or you can just call the django one if you’d prefer
from django.views.debug import default_urlconf
path('', default_urlconf)
I created a new django app and ran it using
python manage.py runserver
. It ran normal and I could see the default page
With the shell outputting:
Django version 4.0.1, using settings 'storefront.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[23/Jan/2022 18:52:07] ←[m"GET / HTTP/1.1" 200 10697←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/css/fonts.css HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0←[0m
Not Found: /favicon.ico
[23/Jan/2022 18:52:07,704] - Broken pipe from ('127.0.0.1', 56807)
Then I added a new app, like so:
python manage.py startapp playground
then I added an action handler
playground/views.py
def say_hello(request):
return HttpResponse('Hello World!')
I also added urls.py
inside the playground
folder
playground/urls.py
urlpatterns = [
path('hello/', views.say_hello)
]
And the main urls.py
I added:
root/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('playground/', include('playground.urls')),
]
In the main settings.py, I included playground
in the INSTALLED_APPS
list.
Then I ran the application, and I can confirm that the path 127.0.0.1:800/playground/hello
works as expected.
However, the empty path 127.0.0.1:8000/
is no longer valid. I get this error:
The empty path only works if I comment the playground
urls,
urlpatterns = [
path('admin/', admin.site.urls),
# once the next line is commented, the empty path works fine
# path('playground/', include('playground.urls')),
]
Here is what the log displays once the empty path is requested:
Django version 4.0.1, using settings 'storefront.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[23/Jan/2022 18:47:36] ←[33m"GET / HTTP/1.1" 404 2174←[0m
Not Found: /favicon.ico
[23/Jan/2022 18:47:36,801] - Broken pipe from ('127.0.0.1', 56786)
What am I doing wrong?
You didn’t do anything wrong, the default django page only shows if you don’t have any other url patterns to try (Github)
Now you need to add a path at /
or you can just call the django one if you’d prefer
from django.views.debug import default_urlconf
path('', default_urlconf)