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
Tutorial #1
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
]
Tutorial #2
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
- Why there is sometimes api and sometimes full path my_project.my_project.api?
- Should I use full path always or short one?
- Why in first tutorial we have used urlpatterns in my_project\api\urls.py and not in tutorial #2?
- Which of this tutorials is closer to best practices?
- Why one time we have
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!