我知道它感觉像是基本的,但我不能提出一个基于doc的干净解决方案。 我有以下项目结构(为了保持问题简洁,省略了models.py,forms.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)
答案 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'),
#...
]