【发布时间】:2011-06-30 19:17:48
【问题描述】:
刚接触 Django,遇到了一个奇怪的问题:Firefox 完全按照我们应用程序中建立的 URL 模式运行,但 Safari 出现错误提示:
Django tried these URL patterns, in this order:
^admin/doc/
^admin/(.*)
^auth/
^game/
^static/(?P<path>.*)$
The current URL, , didn't match any of these.
所以看起来没有匹配的 URL,但为什么它适用于一个客户端而不适用于另一个? Safari 有什么不同?
编辑后包含 game/urls.py 和根级别的 urls.py(应该有 2 个文件吗?):
(游戏/urls.py)
from django.conf.urls.defaults import *
urlpatterns = patterns('game.views',
(r'^$', 'index'),
(r'^dashboard/', 'dashboard'),
(r'^details/(?P<venue_id>\d+)/$', 'details'),
)
(urls.py)
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^starsquare/', include('starsquare.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#(r'^admin/(.*)', include(admin.site.urls)),
(r'^admin/(.*)', admin.site.root),
(r'^auth/', include('djangofoursquare.urls')),
#game
(r'^game/',include('game.urls')),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/home/username/dev/starsquare/game/static'})
)
【问题讨论】:
-
以“Django 已尝试...”开头的部分是在这种情况下您将从 Django 看到的错误消息;它也很可能认为当前 URL 什么都不是。
-
您确定在两个浏览器中使用完全相同的 URL、尾部斜杠等等?
-
如果当前 URL 什么都不是,那么尝试将规则 r"^$" 附加到您想要查看的默认视图中。根据我自己的经验,在所有规则的底部添加这个,让 Django 在没有找到匹配项后抓取它。
标签: django url safari design-patterns