【问题标题】:Django Url Dispatcher issue when using a prefix使用前缀时的 Django Url Dispatcher 问题
【发布时间】:2012-02-08 15:33:53
【问题描述】:

工作:

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

我是按照文档去做的,我哪里出错了?

【问题讨论】:

    标签: django django-views django-urls


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢我错过了!
    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多