django urls.py无法处理网址

时间:2012-01-01 20:34:02

标签: python django linux debian

我正在创建一个django应用程序,我的项目名称是domain_com,应用程序名称是gallery。该项目映射到domain.com,所以当我创建urls.py与这些重定向时,它给我这些错误

(r'^domain_com/(?P<page_name>[^/]+)/edit/$', 'domain_com.gallery.views.edit_page'),
(r'^domain_com/(?P<page_name>[^/]+)/save/$', 'domain_com.gallery.views.save_page'),
(r'^domain_com/(?P<page_name>[^/]+)/$', 'domain_com.gallery.views.view_page')

错误:

Using the URLconf defined in domain_com.urls, Django tried these URL patterns, in this order: 

^domain_com/(?P<page_name>[^/]+)/edit/$ 
^domain_com/(?P<page_name>[^/]+)/save/$ 
^domain_com/(?P<page_name>[^/]+)/$ 
The current URL, edit, didn't match any of these.

知道问题出在哪里?在创建应用程序后,我的django初始安装工作,所以我确定它是urls.py

这是我的apache配置

<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/www.domain.com/htdocs/
ErrorLog /var/www/www.domain.com/logs/error.log
CustomLog /var/www/www.domain.com/logs/access.log combined
SetHandler mod_python
PythonHandler django.core.handlers.modpython
PythonPath sys.path+['/var/app/virtual/']
SetEnv DJANGO_SETTINGS_MODULE domain_com.settings
SetEnv PYTHON_EGG_CACHE /tmp
<Location "/gallery/">
SetHandler None
</Location>
</VirtualHost>

3 个答案:

答案 0 :(得分:3)

您创建了http://domain.com/domain_com/page_name/edit/形式的复杂网址。然而,您正在使用网址http://domain.com/edit进行测试。显然,那些不匹配。

答案 1 :(得分:1)

更新我的回答后:

试试这个:

(r'^/edit/(?P<page_name>\w+)$', 'gallery.views.edit_page'),
(r'^/save/(?P<page_name>\w+)$', 'gallery.views.save_page'),
(r'^/(?P<page_name>\w+)$', 'gallery.views.view_page')

虽然urls.py是您的应用程序的根文件夹。

然后,如果你访问:

http://domain.com/edit/page1

它应该有用

答案 2 :(得分:0)

设置两个主要根网址,以包含应用的网址:https://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-urlconfs