Django - 字典更新序列元素#0 的长度为 1; 2是必需的[重复]
Posted
技术标签:
【中文标题】Django - 字典更新序列元素#0 的长度为 1; 2是必需的[重复]【英文标题】:Django - Dictionary update sequence element #0 has length 1; 2 is required [duplicate] 【发布时间】:2015-03-08 22:27:12 【问题描述】:创建新网址后,出现此错误
/blog/ 字典更新序列元素#0 处的ValueError 长度为1; 2 是必需的
追溯
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 1.7.2
Python Version: 3.4.1
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'modeltranslation',
'blog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
98. resolver_match = resolver.resolve(request.path_info)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py" in resolve
345. sub_match = pattern.resolve(new_path)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py" in resolve
345. sub_match = pattern.resolve(new_path)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py" in resolve
222. kwargs.update(self.default_args)
Exception Type: ValueError at /blog/
Exception Value: dictionary update sequence element #0 has length 1; 2 is required
我用这个网址
# /project/urls.py
from blog.urls import urlpatterns as blog_urls
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include(blog_urls, 'blog')),
)
# /blog/urls.py
urlpatterns = patterns('blog.views',
url(r'^$', 'index', 'index'),
url(r'^category/(?P<category_id>\d+)/$', 'category', 'category'),
)
# /blog/views.py
def index(request):
render(request, 'blog/index.html')
def category(request, category_id):
render(request, 'blog/index.html')
为什么我得到这个错误?以前从未见过,我不知道为什么会出现此错误...
【问题讨论】:
【参考方案1】:问题出在这一行:
url(r'^category/(?P<category_id>\d+)/$', 'category', 'category')
第三个位置参数是额外的上下文字典。如果您想传递名称,则需要使用关键字 arg:
url(r'^category/(?P<category_id>\d+)/$', 'category', name='category')
【讨论】:
谢谢,添加 'name=' 就成功了! 任何人都可以忘记并浪费大量时间调试它们的小东西! 谢谢,真的为我节省了很多时间。以上是关于Django - 字典更新序列元素#0 的长度为 1; 2是必需的[重复]的主要内容,如果未能解决你的问题,请参考以下文章
Elastic Beanstalk:字典更新序列元素 #0 的长度为 1; 2 是必需的
Spark DataFrame to Dict - 字典更新序列元素错误
2021-12-02:给定一个字符串str,和一个正数k。 返回长度为k的所有子序列中,字典序最大的子序列。 来自腾讯。