zoukankan      html  css  js  c++  java
  • django 不能直接把列表给模板 TypeError: context must be a dict rather than tuple.

     将list 传递到前台模板:
     
     举例:
    
    views.py
    
    return render(request,'index.html',list)
    
    def viewtbs(req):
       conn= MySQLdb.connect(
            host='127.0.0.1',
            port = 3306,
            user='root',
            passwd='1234567',
            db ='DEVOPS',
            charset="UTF8"
            )
       cursor = conn.cursor()
       a = cursor.execute("select sn,ip,info,env,script from publish_info;" )
       info = cursor.fetchall()
       print info
       print type(info)
       return render(req,'viewtbs.html',info)
       
    TypeError at /polls/test002/
    
    context must be a dict rather than tuple.
    
    <type 'tuple'>
    Internal Server Error: /polls/test002/
    Traceback (most recent call last):
      File "/usr/local/python27/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
        response = get_response(request)
      File "/usr/local/python27/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "/usr/local/python27/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/exam/mysite/polls/views.py", line 115, in viewtbs
        return render(req,'viewtbs.html',info)
      File "/usr/local/python27/lib/python2.7/site-packages/django/shortcuts.py", line 30, in render
        content = loader.render_to_string(template_name, context, request, using=using)
      File "/usr/local/python27/lib/python2.7/site-packages/django/template/loader.py", line 68, in render_to_string
        return template.render(context, request)
      File "/usr/local/python27/lib/python2.7/site-packages/django/template/backends/django.py", line 64, in render
        context = make_context(context, request, autoescape=self.backend.engine.autoescape)
      File "/usr/local/python27/lib/python2.7/site-packages/django/template/context.py", line 287, in make_context
        raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
    TypeError: context must be a dict rather than tuple.
    [20/Aug/2018 15:36:01] "GET /polls/test002/ HTTP/1.1" 500 94473
  • 相关阅读:
    Linux Ubuntu安装Mysql5.7
    Linux Ubuntu安装maven3.3.9
    Linux Ubuntu安装tomcat9
    Linux Ubuntu安装JDK1.8
    Win10 U盘安装ubuntu16.04 LTS 双系统
    Linux Mysql5.7 常用语句与函数
    在Linux CentOS 6.6上安装Python 2.7.9
    CentOS6下docker的安装和使用
    How to Install Apache Solr 4.5 on CentOS 6.4
    SpringBoot的日志管理
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349083.html
Copyright © 2011-2022 走看看