项目中的每个django应用程序都应该拥有自己的urls.py吗?

时间:2021-11-05 18:06:04

I am working on a django project which will contain several apps. Each app will have their own set of models and views.

我正在开发一个包含多个应用程序的django项目。每个应用程序都有自己的一组模型和视图。

Should each app also define their own url's with a urls.py or maybe a function. What is the best practice for defining urls of apps within a django project, and integrating these urls with the main urls.py (root url conf) ?

每个应用程序是否也应该使用urls.py或函数定义自己的url。在django项目中定义应用程序URL的最佳实践是什么,并将这些URL与主urls.py(root url conf)集成?

3 个解决方案

#1


26  

It depends. If you're dealing with a tiny website with one app, you can keep all the expressions in the same urls.py.

这取决于。如果您正在使用一个应用程序处理一个小网站,您可以将所有表达式保留在相同的urls.py中。

However, when you're dealing with a more complicated site with truly separate apps, I prefer the following structure:

但是,当您使用真正独立的应用程序处理更复杂的网站时,我更喜欢以下结构:

  • myapp
    • admin.py
    • admin.py
    • forms.py
    • forms.py
    • models.py
    • models.py
    • urls.py
    • urls.py
    • views.py
    • views.py
  • myapp admin.py forms.py models.py urls.py views.py
  • manage.py
  • manage.py
  • settings.py
  • settings.py
  • urls.py
  • urls.py

Don't forget each folder needs it's own __ init__.py

不要忘记每个文件夹都需要它自己的__ init__.py

# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    # Notice the expression does not end in $, 
    # that happens at the myapp/url.py level
    (r'^myapp/', include('myproject.myapp.urls')),
)

# myapp/urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('myproject.myapp.views',
    (r'^$', 'default_view',
    (r'^something/$', 'something_view',
)

You also may want to look at Class-based Generic Views

您还可以查看基于类的通用视图

#2


6  

If the app is mostly self-contained and having its own place in the URL hierarchy makes sense, then it should have its own urls.py. But even if it does exist, it's still only a guideline to the project developer unless include() is used to graft it into the project URLconf.

如果应用程序主要是自包含的并且在URL层次结构中有自己的位置是有意义的,那么它应该有自己的urls.py.但即使它确实存在,它仍然只是项目开发人员的指南,除非使用include()将其移植到项目URLconf中。

#3


5  

If your app is going to display anything to the user with its own url pattern, it should probably have its own urls.py file. So in your base urls file, you'd have something in your urlpatterns like url(r'', include('path.to.app.urls')). Then your app's urls.py file would have a pattern like url(r'^$', 'path.to.app.views.view').

如果您的应用程序将使用自己的url模式向用户显示任何内容,则它应该具有自己的urls.py文件。所以在你的基本网址文件中,你的urlpatter中有一些东西,比如url(r'',include('path.to.app.urls'))。然后你的应用程序的urls.py文件将有一个像url(r'^ $','path.to.app.views.view')的模式。

#1


26  

It depends. If you're dealing with a tiny website with one app, you can keep all the expressions in the same urls.py.

这取决于。如果您正在使用一个应用程序处理一个小网站,您可以将所有表达式保留在相同的urls.py中。

However, when you're dealing with a more complicated site with truly separate apps, I prefer the following structure:

但是,当您使用真正独立的应用程序处理更复杂的网站时,我更喜欢以下结构:

  • myapp
    • admin.py
    • admin.py
    • forms.py
    • forms.py
    • models.py
    • models.py
    • urls.py
    • urls.py
    • views.py
    • views.py
  • myapp admin.py forms.py models.py urls.py views.py
  • manage.py
  • manage.py
  • settings.py
  • settings.py
  • urls.py
  • urls.py

Don't forget each folder needs it's own __ init__.py

不要忘记每个文件夹都需要它自己的__ init__.py

# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    # Notice the expression does not end in $, 
    # that happens at the myapp/url.py level
    (r'^myapp/', include('myproject.myapp.urls')),
)

# myapp/urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('myproject.myapp.views',
    (r'^$', 'default_view',
    (r'^something/$', 'something_view',
)

You also may want to look at Class-based Generic Views

您还可以查看基于类的通用视图

#2


6  

If the app is mostly self-contained and having its own place in the URL hierarchy makes sense, then it should have its own urls.py. But even if it does exist, it's still only a guideline to the project developer unless include() is used to graft it into the project URLconf.

如果应用程序主要是自包含的并且在URL层次结构中有自己的位置是有意义的,那么它应该有自己的urls.py.但即使它确实存在,它仍然只是项目开发人员的指南,除非使用include()将其移植到项目URLconf中。

#3


5  

If your app is going to display anything to the user with its own url pattern, it should probably have its own urls.py file. So in your base urls file, you'd have something in your urlpatterns like url(r'', include('path.to.app.urls')). Then your app's urls.py file would have a pattern like url(r'^$', 'path.to.app.views.view').

如果您的应用程序将使用自己的url模式向用户显示任何内容,则它应该具有自己的urls.py文件。所以在你的基本网址文件中,你的urlpatter中有一些东西,比如url(r'',include('path.to.app.urls'))。然后你的应用程序的urls.py文件将有一个像url(r'^ $','path.to.app.views.view')的模式。