Conasty
Conasty

Reputation: 69

Running into 404 error with URL Dispatcher

This is my url.py in WebFetcher

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('Fetcher.urls')),
    path('admin/', admin.site.urls),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This is my url.py in Fetcher

from django.urls import path

from . import views

urlpatterns = [
    path('', views.home, name = 'home'),
    path('page_objects/', views.page_objects, name = 'page_objects')
]

this is my form

<form action="{% url 'page_objects' %}" method="post" enctype="multipart/form-data">

This is the name of my function in views

def page_objects(request):

I am getting 404 error saying Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:

[name='home'] page_ojects [name='page_objects'] admin/ ^media/(?P.*)$ The current path, WebFetcher/page_ojects, didn't match any of these.

I ready all the documentation on the URL Dispatcher and I could not find anything that looks wrong with my code. I hope it is just a syntax error. If you think more of my code will be helpful, comment and I will edit this post.

Edit 1:

I updated my WebFetcher urls.py and my Fetcher urls.py per Daniel's suggest.

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from Fetcher import views

urlpatterns = [
    path('WebFetcher/', include('Fetcher.urls')),
    path('', views.home, name = 'home'),
    path('admin/', admin.site.urls),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



from django.urls import path

from . import views

urlpatterns = [
    path('page_objects/', views.page_objects, name = 'page_objects')
]

Now the 404 error I am getting is

Page not found (404) Request Method: POST Request URL: http://127.0.0.1:8000/WebFetcher/WebFetcher/WebFetcher/page_objects/ Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:

WebFetcher/ page_objects/ [name='page_objects'] [name='home'] admin/ ^media/(?P.*)$ The current path, WebFetcher/WebFetcher/page_objects/, didn't match any of these.

Upvotes: 0

Views: 245

Answers (3)

Daniel
Daniel

Reputation: 3527

You need to go to http://mywebsite/page_objects instead of http://mywebsite/WebFetcher/page_objects.

If you want to have the page_objects url nested under WebFetcher then you can do this to your urls.py:

MyProject/urls.py:

urlpatterns = [
    path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
    path('admin/', admin.site.urls),
]

Fetcher/urls.py:

urlpatterns = [
    path('', views.home, name = 'home'),
    path('page_objects/', views.page_objects, name = 'page_objects')
]

Note: your home page will now be at http://mywebsite/WebFetcher - if you want it to be at the root, i.e. http://mywebsite then you can do this instead:

MyProject/urls.py:

urlpatterns = [
    path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
    path('', views.home, name = 'home'), # move the home page path to the root urls.py
    path('admin/', admin.site.urls),
]

Fetcher/urls.py:

urlpatterns = [
    path('page_objects/', views.page_objects, name = 'page_objects')
]

Upvotes: 1

Shaonsani
Shaonsani

Reputation: 178

please write this pattern that will work fine.

path('page_ojects/', views.page_objects, name = 'page_objects')

Updated: add this code to your project level urls.py

 from django.contrib import admin
 from django.urls import path, include

 urlpatterns = [
 path('admin/', admin.site.urls),
 path('', include('Fetcher.urls')),
 ]

And if it doesn't solve your issue, then there might a issue in your views.py module, maybe your views doesn't find any correct views to dispatch. so write below code for testing purpose,in yours views.py

from django.shortcuts import render

def page_objects(request):

return render("Hello, pages_objects!!")

Upvotes: 0

Ed Kohler
Ed Kohler

Reputation: 544

You have a typo in your urls.py path.

page_ojects should be page_objects

Upvotes: 1

Related Questions