Django网站加载空root urls.py文件

时间:2016-12-18 10:04:58

标签: django urlconf

我知道它感觉像是基本的,但我不能提出一个基于doc的干净解决方案。 我有以下项目结构(为了保持问题简洁,省略了models.py,forms.py等文件)

  • 程序hello_world
    • hellow_world
      • urls.py
    • APP_2
      • urls.py
    • app_3
      • urls.py
    • manage.py
    • urls.py
    • settings.py

如您所见,我的目标是为每个应用分配一个单独的urls.py文件,然后将它们组合到根urls.py中(与列表中的settings.py描述的级别相同)以上)。问题是我的root urls.py现在是EMPTY(!!!),该网站仍然加载主页!我究竟做错了什么 ??? 请参阅以下详细信息:

settings.py:

ROOT_URLCONF = 'urls'

hellow_world urls.py:

urlpatterns = [
    url(r'^$', views.home , name = 'home'),
]

root urls.py - 空!

manage.py:

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

1 个答案:

答案 0 :(得分:1)

使用include()添加更多网址:

# your main urls.py file
from django.conf.urls import include, url

urlpatterns = [
    url(r'^myapp1/', include('myapp1.urls')),
    url(r'^myapp2/', include('myapp2.urls')),
]

# myapp1/urls.py
from django.conf.urls import url

from . import views

app_name = 'myapp1'
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    #...
]