链接Django中的应用程序,哪个更好,urls.py或app.py?

lf5gs5x2  于 2023-01-03  发布在  Go
关注(0)|答案(2)|浏览(109)

作为Django的初学者,我浏览了一些教程,注意到一位导师链接了www.example.com中的各种应用程序urls.py,另一位导师使用config函数在www.example.com中完成了这app.py操作
我只是想在我做任何事之前得到澄清

s2j5cfk0

s2j5cfk01#

djangourls.py文件包含每个应用程序的URL路由。此处不应执行任何其他操作。
实施例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'),
       ...
     ]

并更改common/apps.py文件中的应用程序名称和signal.py文件配置等。

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

kjthegm6

kjthegm62#

我已经想通了,我发现urls.py文件是用来路由和链接各种应用程序的,而apps.py当模型集成到应用程序中时,www.example.com页面就变得非常必要了。

相关问题