django urls.py - 特定于项目和应用程序

时间:2015-03-02 01:04:25

标签: python django

如何配置Django网址解析,使除somedomain.com/admin以外的所有其他网址指向应用程序而不添加任何前缀?

在我的项目urls.py中,我有

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url(r'^$', include("photoapp.urls", namespace="photoapp")),
    url(r'^admin/', include(admin.site.urls)),
)

photoapp/urls.py,我有

from django.conf.urls import patterns, url
from . import views

urlpatterns = patterns('',
    url(r'^$',views.index),
    url(r'^fileUpload/?$', views.file_upload)
)

现在,这两个网址都在photoapp/views.py内有关联的观点。 somedomain.com加载views.index指定的模板,但somedomain.com/fileUpload返回404.具体而言,错误消息说明

Using the URLconf defined in project.urls, Django tried these URL
patterns, in this order:
    ^$
    ^admin/

我的目标是

somedomain.com/admin

解析为管理员应用,

somedomain.com
somedomain.com/fileUpload
somedomain.com/search?q=query

photoapp/urls.py处理。如何配置应用程序urls.py呢?

1 个答案:

答案 0 :(得分:2)

从第一个网址中删除$个符号:

url(r'^', include("photoapp.urls", namespace="photoapp")),