错误:“只能将元组(不是“列表”)连接到元组”在 url 中,Django

发布于 2024-12-06 17:40:17 字数 2212 浏览 2 评论 0原文

我的网址有错误:

/ 处的 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}),
)

Django 配置

回溯:

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'^

Django Config

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>.*)

Django Config

Traceback:


, 'django.views.static.serve',
                                   {'document_root': settings.MEDIA_ROOT}),
    (r'^site_media/static/(?P<path>.*)

Django Config

Traceback:


, 'django.views.static.serve',
                                {'document_root': settings.STATIC_ROOT}),
)

Django Config

Traceback:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

站稳脚跟 2024-12-13 17:40:17

该错误几乎描述了您的问题。您在 urlpatterns 的第一个定义中缺少对 patterns() 的调用。

The error pretty much describes your problem. You're missing a call to patterns() in your first definition of urlpatterns.

撩动你心 2024-12-13 17:40:17

/ 处的 TypeError 只能将元组(而不是“列表”)连接到元组

它的意思正是它所说的。它抱怨urlpatterns +=patterns(...)+= 尝试连接(链接在一起)两个事物。 urlpatterns 是一个元组。 patterns(...) 返回的值是一个列表。您不能将它们混合在一起进行串联。

要解决此问题,您必须首先决定是否需要元组或列表作为结果(连接两个元组给出一个元组,连接两个列表给出一个列表),然后相应地修复一侧或另一侧。

就您而言,您显然需要一个列表。您分配给 urlpatterns 的值首先看起来像是 patterns() 的一组参数。正如 @patrys 指出的,简单的解释是您打算(并且忘记)在此处调用该函数。这将为您提供一个列表,然后您可以将第二次调用中的列表附加(连接)到该列表。

请注意,您也可以一次性完成所有操作:urlpatterns =patterns(...) +patterns(...)

列表在哪里?

patterns() 调用的结果,如上所述(以及文档,大概 - 我对 django 一无所知,我只是擅长调试。)

TypeError at / can only concatenate tuple (not "list") to tuple

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 by patterns(...) 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 for patterns(). 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(...).

Where is the list in there?

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文