Django多个应用程序和urls.py

时间:2016-07-29 11:50:53

标签: django url routes

我有一个使用Django框架设置的站点。我希望这个网站的网址映射如下:

www.site.com                 -> main.html
www.site.com/                -> main.html
www.site.com/projects        -> projects.html
www.site.com/projects/       -> projects.html
www.site.com/projects/web    -> web.html
www.site.com/projects/web/   -> web.html
www.site.com/projects/mobile -> mobile.html
www.site.com/projects/mobile/-> mobile.html
www.site.com/projects/whatev -> 404.html
www.site.com/projects/whenev -> 404.html
www.site.com/projects/web/w  -> 404.html

我目前将主网站设置为具有自己网址的Django应用程序,并将网站的项目部分设置为自己的应用程序。映射如下:

网站/ urls.py

urlpatterns = [
    url(r'^$', portfolio_views.about_page, name = "aboutPage"),
    url(r'^about$', portfolio_views.about_page, name = "aboutPage"),
    url(r'^experience$', portfolio_views.experience_page, name = "experiencePage"),
    url(r'^projects', include('projectsApp.urls')),
    url(r'^projects/', include('projectsApp.urls')),
    url(r'^publications$', portfolio_views.publications_page, name = "publicationsPage"),
    url(r'^qualifications$', portfolio_views.qualifications_page, name = "qualificationsPage"),
    url(r'^contact$',portfolio_views.contact_page, name="contactPage"),
    url(r'^submitted$', portfolio_views.contact_submitted_page, name = "submittedPage"),
    url(r'^admin/', admin.site.urls),
]

对于项目应用程序:

项目/ urls.py

urlpatterns = [
        url(r'^$', views.projects_page, name='projectPage'),#site/projects
        url(r'^web$',views.web_project, name='webProject') #site/projects/web
    ]

目前,以下网址会产生:

www.site.com                 -> main.html
www.site.com/                -> main.html
www.site.com/whatever        -> 404.html (with css formatting)
www.site.com/projects        -> projects.html
www.site.com/projects/       -> projects.html (without css formatting)
www.site.com/projects/web    -> 404.html (without css formatting)
www.site.com/projects/web/   -> 404.html (without css formatting)
www.site.com/projects/web/whatev -> 404.html (without css formatting)

网站App中的/ static / dir中有css格式。

我不知道为什么,鉴于我的网址模式,我无法从项目应用程序中获取/ web。在我的projects.html中,我有一个链接,指向web.html作为' projects / web'产生404.html。我是否需要始终链接主应用程序之外的绝对地址?即在项目应用程序中提供的任何文件为www.site.com/projects/link?

1 个答案:

答案 0 :(得分:0)

include用于没有斜杠的网址(例如r'^projects')非常不寻常。这意味着您要在/projectsweb以及/projects/web上提供内容。

我会尝试从你的url配置中删除没有尾部斜杠的包含。

然后,只要APPEND_SLASH设置为True并且您安装了公共中间件,Django就会从没有斜杠的网址重定向到带斜杠的网址。例如,它会从www.site.com/projects重定向到www.site.com/projects/

这可能会修复您的CSS格式。如果它没有,您将拥有一个简单的URL配置,因此更容易发现其他问题。