Reputation: 9
Hi I am trying to create my first Django project and having issues getting an app to work inside my Django project. I simply am trying to map the url code 8000/home1/hello/ to display the basic text "hello world" but failing to do so. I keep getting this error code:
"Using the URLconf defined in web3.urls, Django tried these URL patterns, in this order:
admin/ The current path, home1/hello, didn’t match any of these."
Here is what I have done:
from django.urls import path from . import views
urlpatterns = [
path('hello/', views.hello_world, name="hello_world"),
]
from django.shortcuts import render
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("you finally did it justin")
6.web3> urls.py includes this code:
from django.contrib import admin
from django.urls import path, include
from home1.views import hello_world # Import the hello_world view function
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home1.urls')),
]
I am fully lost on this one... any help would be greatly appreciate!!!
Upvotes: 0
Views: 495
Reputation: 390
the main issue is that the URL pattern home1/hello/
is not matching because the web3/urls.py
is including home.urls
at the root URL path this means home1/hello/
will actually be matched as hello/
from the root
to fix this you should do these steps:
web3/
manage.py
web3/
__init__.py
settings.py
urls.py
wsgi.py
home1/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
urls.py
migrations/
__init__.py
home1
is added to INSTALLED_APPS
home1/urls.py
should looks like this :from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.hello_world, name='hello_world'),
]
home1.urls
in web3/urls.py
and use home1/
as prefix:from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('home1/', include('home1.urls')),
]
and finally your home1/views.py
should looks like this:
from django.shortcuts import render
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("you finally did it justin")
other things that comes to my mind:
python manage.py check
from django.urls import resolve
print(resolve('/home1/hello/'))
I hope this helps you.
Upvotes: 0
Reputation: 4091
The path "home1/hello/"
does not exist on your Server. Only "hello/"
and "admin/"
exists.
It is a common misconception that if you include a URLconf into your root URLconf like so:
urlpatterns = [
#•••Rest of code•••
path('', include('home1.urls')),
]
That all routes from that app will begin with the name of the app —e.g. "home1"
in the above.
This is not true. For there to exist a basename, you must give a basename.
urlpatterns = [
#•••Rest of code•••
path('home1/', include('home1.urls')),
]
Now all routes from that app will begin with "home1"
.
Upvotes: 0