当“包含”Django URL模式时,'function'对象不是可下标的? [英] 'function' object is not subscriptable when "include"ing Django URL pattern?

查看:133
本文介绍了当“包含”Django URL模式时,'function'对象不是可下标的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Django网站,关于 http://youtube.com 上正在看的教程,我有问题。这是我的urls.py脚本:

  from django.conf.urls import patterns,include,url 

#取消注释下面两行以启用admin:
#from django.contrib import admin
#admin.autodiscover()

urlpatterns = patterns('',
(r'^',include ['FirstBlog.apps.homepage.urls']),
#示例:
#url(r'^ $','FirstBlog.views.home' name ='home'),
#url(r'^ FirstBlog /',include('FirstBlog.foo.urls')),

#取消下面的admin / doc行的注释启用管理文档:
#url(r'^ admin / doc /',include('django.contrib.admindocs.urls')),

#取消注释下一行以启用admin:
#url(r'^ admin /',include(admin.site.urls)),

我的输出:

  Type $ at / 
'function'object is不可下标
请求方法:GET
请求URL:http://127.0。 0.1:8000 /
Django版本:1.4
异常类型:TypeError
异常值:
'函数'对象不可下标
异常位置:/ Users / thor /第/ 8行中的网站/ FirstBlog / FirstBlog / urls.py
Python可执行文件:/ usr / bin / python
Python版本:2.7.1
Python路径:
['/ Users / thor / Sites / FirstBlog',
'/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 ',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions /2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/额外/ lib / python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/系统/库/框架/ Python.framework / Versions / 2.7 / lib / python2.7 / lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload'
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
服务器时间:2012年5月2日,星期三17:42:04 -0500

请帮忙? >

另外,如果你有一个很好的教程,我可以关注,请链接我!另外,我最好还是用Mamp Pro来做数据库。帮助n00b?

解决方案

include 是一个函数;您正在下标,就像它是一个列表。



例如:

  apples = ['Granny Smith','Russet'] 
red_apple = apples [1]#< - 下标列表

由于函数不能被下标,您会收到您发布的错误。但是,从注释的示例中可以看出,您应该使用()语法调用函数:

 (r'^',include('FirstBlog.apps.homepage.urls'))
#^ ^


I'm making a Django website, off of a tutorial I'm watching on http://youtube.com, and I'm having problemos. Here's my urls.py script:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    (r'^', include['FirstBlog.apps.homepage.urls']),
    # Examples:
    # url(r'^$', 'FirstBlog.views.home', name='home'),
    # url(r'^FirstBlog/', include('FirstBlog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

And my output:

TypeError at /
'function' object is not subscriptable
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.4
Exception Type: TypeError
Exception Value:    
'function' object is not subscriptable
Exception Location: /Users/thor/Sites/FirstBlog/FirstBlog/urls.py in <module>, line 8
Python Executable:  /usr/bin/python
Python Version: 2.7.1
Python Path:    
['/Users/thor/Sites/FirstBlog',
 '/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages']
Server time:    Wed, 2 May 2012 17:42:04 -0500

Please help?

Also, if you have a good tutorial I could follow, please link me it! Also, I would preferably like to use Mamp Pro for the databases. Help a n00b?

解决方案

include is a function; you're subscripting it as if it was a list.

For example:

apples = [ 'Granny Smith', 'Russet' ]
red_apple = apples[1]    # <-- Subscripting a list

Since functions can't be subscripted, you get the error you posted. But, as can be seen in the commented examples, you should be calling the function using the () syntax:

(r'^', include('FirstBlog.apps.homepage.urls'))
#             ^                              ^

这篇关于当“包含”Django URL模式时,'function'对象不是可下标的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆