Reputation: 23
reading few similar topics in stackoverflow and https://docs.djangoproject.com/en/3.2/ref/applications/#configuring-applications Im still facing issue understanding proper way of handling urls. I'm following two REST API tutorials in which they differ from each other about urls.py's, settings.py, apps.py files.
Having structure like this:
├───my_project
│ └───api
│ ├───apps.py
| └───urls.py
│ └───my_project
| ├───urls.py
| └───settings.py
| └───manage.py
my_project\api\apps.py
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api" ########### DIFF
my_project\api\urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(prefix="symbols", viewset=views.SymbolsViewSet)
urlpatterns = [ ########### DIFF
path("", include(router.urls)), ########### DIFF
] ########### DIFF
my_project\my_project\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include("api.urls")), ########### DIFF
]
my_project\my_project\settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"api", ########### DIFF
]
my_project\api\apps.py
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "my_project.my_project.api" ########### DIFF
my_project\api\urls.py
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(prefix="symbols", viewset=views.SymbolsViewSet)
########### DIFF Lack of 3 lines
my_project\my_project\urls.py
from django.contrib import admin
from django.urls import path, include
from my_project.my_project.api.urls import router
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include(router.urls)), ########### DIFF
]
my_project\my_project\settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"my_project.my_project.api",
]
I have put
########### DIFF where files are different and bolded text.
So the main interesting difference is in the file my_project\my_project\urls.py
path("api/", include("api.urls")),
or
path("api/", include(router.urls)),
To get same return from include for the first one I need:
include("my_project.my_project.api.urls")[0].router.urls[0]
for the second one
router.urls[0]
so why "api.urls" is working?
Hope that some questions are clear, thank you for any tips & help! Have a good day!
Upvotes: 2
Views: 763
Reputation: 238
We use ”include” in the main directory case we have created another urls.py for apps.
I personally prefer to have urls.py of each app individually and include them in main directory.
Imagine you wanna share this app with someone or publish it to pip or re-use it in another project, wouldn’t be more realistic to have full functional app with all its urls and resources inside one folder.
Upvotes: 0