首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >构建字典列表提供了一个ValueError

构建字典列表提供了一个ValueError
EN

Stack Overflow用户
提问于 2017-03-06 04:33:40
回答 1查看 56关注 0票数 0

我在构建像这样的字典列表时遇到了问题

代码语言:javascript
复制
@login_required(login_url="login/")
def home(request):
    user = StaticHelpers.user_to_subclass(request.user)[1]
    user_type = StaticHelpers.user_to_subclass(request.user)[0]

    if user_type == "Patient":
        apps = StaticHelpers.find_appointments(user)
        events = []
        for app in apps:
            events.append({
                'title': str(app.doctor),
                'start': str(app.start_time),
                'end': str(app.end_time)
            })
        return render(request, 'HealthApp/patientIndex.html', events)
    elif user_type == "Doctor" or user_type == "Nurse":
        return render(request, 'HealthApp/doctorIndex.html')
    elif user_type == "Admin":
        return render(request, 'HealthApp/doctorIndex.html')

每个字典都需要有这3个值,我需要它们的列表。然而,它却向我抛出了这个错误

代码语言:javascript
复制
Internal Server Error: /
Traceback (most recent call last):
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/devin-matte/Documents/Healthnet/trunk/HealthApp/views.py", line 23, in home
    return render(request, 'HealthApp/patientIndex.html', events)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/shortcuts.py", line 67, in render
    template_name, context, request=request, using=using)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/template/loader.py", line 97, in render_to_string
    return template.render(context, request)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/template/backends/django.py", line 92, in render
    context = make_context(context, request)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/template/context.py", line 291, in make_context
    context.push(original_context)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/template/context.py", line 61, in push
    return ContextDict(self, *dicts, **kwargs)
  File "/home/devin-matte/.local/lib/python3.5/site-packages/django/template/context.py", line 20, in __init__
    super(ContextDict, self).__init__(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 3; 2 is required
[05/Mar/2017 19:56:17] "GET / HTTP/1.1" 500 99419
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-06 05:00:54

回溯显示,问题与创建字典无关,而是与如何将它们发送到模板有关。render的第三个参数必须是字典,其中的键是您希望用来在模板中引用该值的名称。所以:

代码语言:javascript
复制
return render(request, 'HealthApp/patientIndex.html', {"events": events})

现在您可以遍历模板中的events

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42613624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档