错误:“只能将元组(不是“列表”)连接到元组”在 url 中,Django
我的网址有错误:
/ 处的 TypeError 只能将元组(而不是“列表”)连接到元组
无法明白我做错了什么。里面的列表在哪里?
from django.conf import settings
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = ('googleapi.apiapp.views',
(r'^$', 'first_view'),
)
urlpatterns += patterns('',
# Uncomment the admin/doc line below 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)),
# Static files url.
(r'^site_media/media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
(r'^site_media/static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)
回溯:
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
250. for pattern in self.url_patterns:
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_url_patterns
279. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_urlconf_module
274. self._urlconf_module = import_module(self.urlconf_name)
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/home/i159/workspace/apiroot/googleapi/../googleapi/urls.py" in <module>
24. {'document_root': settings.STATIC_ROOT}),
Exception Type: TypeError at /
Exception Value: can only concatenate tuple (not "list") to tuple
I've got an error in my urls:
TypeError at / can only concatenate tuple (not "list") to tuple
Can't get what I did wrong. Where is the list in there?
from django.conf import settings
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = ('googleapi.apiapp.views',
(r'^
Traceback:
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
250. for pattern in self.url_patterns:
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_url_patterns
279. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_urlconf_module
274. self._urlconf_module = import_module(self.urlconf_name)
File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/home/i159/workspace/apiroot/googleapi/../googleapi/urls.py" in <module>
24. {'document_root': settings.STATIC_ROOT}),
Exception Type: TypeError at /
Exception Value: can only concatenate tuple (not "list") to tuple
, 'first_view'),
)
urlpatterns += patterns('',
# Uncomment the admin/doc line below 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)),
# Static files url.
(r'^site_media/media/(?P<path>.*)
Traceback:
, 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
(r'^site_media/static/(?P<path>.*)
Traceback:
, 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)
Traceback:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误几乎描述了您的问题。您在
urlpatterns
的第一个定义中缺少对patterns()
的调用。The error pretty much describes your problem. You're missing a call to
patterns()
in your first definition ofurlpatterns
.它的意思正是它所说的。它抱怨
urlpatterns +=patterns(...)
。+=
尝试连接(链接在一起)两个事物。urlpatterns
是一个元组。patterns(...)
返回的值是一个列表。您不能将它们混合在一起进行串联。要解决此问题,您必须首先决定是否需要元组或列表作为结果(连接两个元组给出一个元组,连接两个列表给出一个列表),然后相应地修复一侧或另一侧。
就您而言,您显然需要一个列表。您分配给
urlpatterns
的值首先看起来像是patterns()
的一组参数。正如 @patrys 指出的,简单的解释是您打算(并且忘记)在此处调用该函数。这将为您提供一个列表,然后您可以将第二次调用中的列表附加(连接)到该列表。请注意,您也可以一次性完成所有操作:
urlpatterns =patterns(...) +patterns(...)
。patterns()
调用的结果,如上所述(以及文档,大概 - 我对 django 一无所知,我只是擅长调试。)It means exactly what it says. It is complaining about
urlpatterns += patterns(...)
. The+=
attempts to concatenate (chain together) two things.urlpatterns
is a tuple. The value returned bypatterns(...)
is a list. You can't mix these for concatenation.To fix this, you must first decide whether you want a tuple or a list as the result (concatenating two tuples gives a tuple, and concatenating two lists gives a list), and then fix one side or the other accordingly.
In your case, you apparently want a list. The value you assign to
urlpatterns
first looks like a set of arguments forpatterns()
. The simple explanation, as @patrys points out, is that you meant (and forgot) to call the function here. That would give you a list, to which you could then append (concatenate) the list from the second call.Note that you can also do it all in one go:
urlpatterns = patterns(...) + patterns(...)
.The result of the
patterns()
calls, as explained above (and also by the documentation, presumably - I don't know anything about django, I'm just good at debugging.)