Another John Doe
Another John Doe

Reputation: 23

Different django rest approach for urls.py, INSTALLED_APPS, apps.py

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

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

Upvotes: 2

Views: 763

Answers (1)

Yousef Alm
Yousef Alm

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

Related Questions