使用前缀时Django Url Dispatcher问题

时间:2012-02-08 15:33:53

标签: django django-views django-urls

工作:

urlpatterns = patterns('',
    (r'^$', views.index),
    (r'^test/$|test/(\d+)/$', views.test_page),
    (r'^(name)/$', views.index),
    (r'^(username)/$', views.index),
)

不工作:

urlpatterns = patterns('views',
    (r'^$', index),
    (r'^test/$|test/(\d+)/$', test_page),
    (r'^(name)/$', index),
    (r'^(username)/$', index),
)

错误:

Django Version:     1.3
Exception Type:     NameError
Exception Value:    name 'index' is not defined
Exception Location: /home/nolhian/Documents/Test/../test/urls.py in <module>, line 8

我跟着文档做了这个,我哪里出错了?

1 个答案:

答案 0 :(得分:5)

如果使用前缀,则必须将视图指定为字符串:

urlpatterns = patterns('views',
    (r'^$', 'index'),
    (r'^test/$|test/(\d+)/$', 'test_page'),
    (r'^(name)/$', 'index'),
    (r'^(username)/$', 'index'),
)