Home > Enterprise >  linking apps in Django, which is better, urls.py or app.py?
linking apps in Django, which is better, urls.py or app.py?

Time:12-31

Going through some tutorials as a beginner in Django, I noticed a tutor linked the various apps in the urls.py and another tutor did it with the app.py using the config function

just looking for clarification before I try anything

CodePudding user response:

The django urls.py file contains the URL routing for each app. No other work should be done here.

example common/urls.py:

from django.urls import path
   urlpatterns = [
       path('index/', views.index, name='main-view'),
       path('bio/<username>/', views.bio, name='bio'),
       path('articles/<slug:title>/', views.article, name='article-detail'),
       path('articles/<slug:title>/<int:section>/', views.section, name='article- 
       section'),
       ...
     ]

And change app name in common/apps.py file, and signal.py file config etc.

from django.apps import AppConfig
class CommonConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'common'
    verbose_name = "Settings"

    def ready(self):
        import .common.signals

more

CodePudding user response:

I have figured it out. I found out that the urls.py file is for routing and linking the various apps while the apps.py pages becomes quite necessary to use when models have been integrated into the apps.

  • Related