2

I believe this is a simple question but I am having a hard time figuring out why this is not working.

I have a django project and I've added a second app (sales). Prior to the second app, my urls.py simply routed everything to the first app (chart) with the following:

urlpatterns = [

    path('admin/', admin.site.urls),
    path('', include('chart.urls')),
]

and it worked fine.

I have read the docs over and over a looked at many tutorials, so my impression is that I can simply amend the urls.py to include:

urlpatterns = [

    path('admin/', admin.site.urls),
    path('sales/', include('sales.urls')),
    path('', include('chart.urls')),
]

and it should first look for a url with sales/ and if it finds that then it should route it to sales.urls, and if it doesn't find that then move on and route it to chart.urls. However, when I load this and type in 127.0.0.1:8000/sales, it returns the following error:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/sales/
Raised by:  chart.views.index

which tells me that it is not routing my sales/ url to sales.urls but to chart.urls. When I change path('', include('chart.urls')), to path('', include('sales.urls')), it does route it to sales.urls so I know that my sales.urls file is set up correctly.

I know this is probably an easy question but I cannot figure it out with anything I've read. Any help is appreciated.

chart.urls:

from django.urls import path, re_path
from . import views

urlpatterns = [
    path('dashboard/', views.chart, name='dashboard'),
    path('', views.index, name='index', kwargs={'pagename': ''}),
    path('<str:pagename>/', views.index, name='index'),
]

sales.urls:

from django.urls import path
from . import views

urlpatterns = [
    path('sales/', views.sales, name='Sales Dashboard'),
]
5
  • Have you added the apps in "INSTALLED_APPS " in settings.py?
    – filiphl
    Commented Aug 7, 2018 at 10:50
  • So there is an ambiguity between URLs? Two views can have the same URL? Don't do that! It will break reverse(..) functions. Actually it is better that regardless of the order of the URLs, every URL has exactly one view and vice versa (relative to view parameters of course). Commented Aug 7, 2018 at 10:53
  • What is in sales.urls and chart.urls? Exactly what pattern are you expecting to serve the /sales/ request? Commented Aug 7, 2018 at 10:57
  • @Chris I couldn't reproduce the behavior. Can you add the contents of sales.urls and chart.urls
    – JPG
    Commented Aug 7, 2018 at 11:52
  • I've added the chart.urls and sales.urls. Individually those both work when the myapp.urls actually routes to them, but I clearly and missing something. I appreciate all the feedback.
    – Chris
    Commented Aug 7, 2018 at 16:14

3 Answers 3

1

I think the correct url for the sales page in your case would be http://127.0.0.1:8000/sales/sales/.

This is because sales/ is then looking in the included sales.urls and not finding any urls at the root / (the only url included there is at sales/ (within sales/) so will be sales/sales/

Reply to an OLD question but may be useful for others...

1
from django.urls import path, include

dont for get to import include

-2

Do this:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('chart.urls')),
    url(r'^sales/', include('sales.urls')),
]

you are not doing it properly since you are not telling your app where go once it is loaded .this r'^' to go directly to chart.url url(r'^', include('chart.urls')),

1
  • 2
    '' and '^' are exactly equivalent. Commented Aug 7, 2018 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.