【问题标题】:django urls.py cannot process the urldjango urls.py 无法处理 url
【发布时间】:2012-01-01 20:34:02
【问题描述】:

我正在创建一个 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>

【问题讨论】:

标签: python django linux debian


【解决方案1】:

您创建了一个复杂的 URL,格式为 http://domain.com/domain_com/page_name/edit/。然而,您正在使用 URL http://domain.com/edit 进行测试。显然,这些不匹配。

【讨论】:

    【解决方案2】:

    更新我的答案后:

    试试这个:

    (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

    它应该可以工作

    【讨论】:

    • 仍然给出 404 错误 domain.com/edit/page_name1 或我在编辑/页面名称中输入的任何内容
    • 1) 确保您的应用程序位于 settings.py 中已安装的应用程序中 2) 在根 urls.py 中确保您包含您的应用程序 urls.py 3) 然后在您的应用程序 url 中使用相对 url .py
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2023-01-18
      • 2016-10-22
      • 2014-01-03
      • 2014-01-08
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多