0

I am a beginner in Django I have created a Django project in that I have included two more application when I did add urls.py file in both Applications both are working well but when I am fetching my main admin URL it is giving an error

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

admin/
shop/
blog/
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.

when I am fetching this URL in http://127.0.0.1:8000/ i am getting an error it is working for http://127.0.0.1:8000/shop/

here is my main urls.py

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

urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
James Z
  • 12,209
  • 10
  • 24
  • 44
bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22

3 Answers3

3

Your django app has 3 routes:
http://127.0.0.1:8000/admin/ goes to django admin app
http://127.0.0.1:8000/shop/ goes to your shop app
http://127.0.0.1:8000/blog/ goes to your blog app

And since you have no configuration for http://127.0.0.1:8000, you see an error instead.
You can see that in the error, when django tries to match your url with list of available urls.

If you want to get admin app on url http://127.0.0.1:8000, change urls.py to:

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

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

It's generally not advisable to set admin app at root url - it has it's own system of urls inside (admin/<app_name>/<model_name>), so chances are it will shadow your urls and make the unreachable.

Gasanov
  • 2,839
  • 1
  • 9
  • 21
2

Create a view that will be your front page. From there you should link to the other areas of your website. Don't direct that to admin, that's ridiculous.

LotB
  • 461
  • 4
  • 10
0

You've created a Django project and started two apps. You should have a project-level urls.py file and then an app-level urls.py file for each of your apps.

To explain that in greater detail lets say our Django project is called config and our two apps are called app1 and app2. Your project-level urls.py file, which will be located at config/urls.py, could contain the following:

    # config/urls.py

    from django.contrib import admin
    from django.urls import path, include
    from django.views.generic.base import TemplateView

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', TemplateView.as_view(template_name='home.html'),
            name='home'),
        path('app1/', include('app1.urls')),
        path('app2/', include('app2.urls')),
    ]

In this file we specify a route for our admin panel, which on your local server will be located at http://127.0.0.1:8000/admin. We've also specified a home route, the second path with the empty string. This means when you navigate to http://127.0.0.1:8000/ you will be directed to your home page (for the example above I just used a generic built-in view). It's not a good idea to route immediately to your admin panel.

We've also included paths to our other two apps. What these two lines essentially say is: "include the urls from this other app". Now we need to create two urls.py files, one for each of our apps. In this example I'll just focus on the urls.py file for app1:

    # app1/urls.py

    from django.urls import path
    from .views import AppContentView

    urlpatterns = [
        path('content/', AppContentView.as_view(), 
            name='app_content'),
    ]

This is a view that you would have to create, but what we've now done is we've created one path that will be located at http://127.0.0.1:8000/app1/content. In fact any new paths we create in this file will always begin with http://127.0.0.1:8000/app1/, because we've already told Django in our project-level urls.py to include the urls from the app1 urls.py file so we've essentially prefixed all of these paths with /app1/.

If you think of urls configurations like a tree it might help too:

                  Project Level Url Configs.
                              |
                              |                                  
                              |
                 ___________________________
                 |                          |                              
                 |                          |
          App 1 Url Configs.          App 2 Url Configs.