从 db 获取数据时,django 中的简单循环出现奇怪错误

Posted

技术标签:

【中文标题】从 db 获取数据时,django 中的简单循环出现奇怪错误【英文标题】:strange error in simple loop in django while getting data from db 【发布时间】:2012-08-14 22:59:05 【问题描述】:

我从 django 模型中获取数据并使用 for 循环在模板中显示数据,但它说:TypeError at /jobs/ __init__() takes exactly 1 argument (8 given)

这是我要获取数据的视图代码:

jobs_cities=City.objects.filter(job__isnull=False).distinct().annotate(jobs_count=models.Count('job')).order_by('jobs_count')[:6]

然后我使用以下视图文件行将此数据传递给模板:

return render_to_response('jobs/list.html', locals(),context_instance=RequestContext(request))

然后在模板中显示为:

% for job_city in jobs_cities %
  <li><a href="#">Jobs in job_city.name </a></li>
 % endfor %

堆栈跟踪:

Environment:


Request Method: GET
Request URL: http://localhost:8000/jobs/

Django Version: 1.4.1
Python Version: 2.6.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'jobs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template /media/../templates/jobs/list.html, error at line 93
   __init__() takes exactly 1 argument (8 given)
   83 : <div class="clear"></div>


   84 :   


   85 :   </div><!--float_left-->


   86 :   


   87 :   <div class="float_right">


   88 :   <div class="post_btn"><a href="#"><img src="STATIC_URLimages/post_btn.gif" border="none" ><br><img src="STATIC_URLimages/ffree_text.gif"></a></div><!--post_btn-->


   89 :   <div class="create_prfoile_btn"><a href="#"><img src="STATIC_URLimages/banner.gif"  border="none"></a></div><!---create_prfoile_btn-->


   90 :   <div class="clear"></div>


   91 :   <div class="featured_jobs">


   92 :   <ul>


   93 :      % for job_city in jobs_cities % 


   94 :   <li><a href="#">Jobs in job_city.name </a></li>


   95 :     % endfor %


   96 :   </ul>


   97 :   


   98 :   </div><!--featured_jobs-->


   99 :   


   100 :   </div><!--float_right-->


   101 :   


   102 :   


   103 :   <div class="clear"></div>


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/media/Local Disk/programming/django_projects/kaasib/jobs/views.py" in list
  38.     return render_to_response('jobs/list.html', locals(),context_instance=RequestContext(request))
File "/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in render_to_string
  176.         return t.render(context_instance)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/defaulttags.py" in render
  145.         len_values = len(values)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in __len__
  85.                 self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in iterator
  301.                     obj = model(*row[index_start:aggregate_start])

Exception Type: TypeError at /jobs/
Exception Value: __init__() takes exactly 1 argument (8 given)

我还查看了调试代码,它显示 jobs_cities 是空列表,所以我想知道我实际上做错了什么?

提前谢谢各位。

【问题讨论】:

有趣。您是否在 City 模型类中覆盖 __init__?也许展示你的一些models.py 可能会更清楚一些。 【参考方案1】:

您似乎覆盖了CityJob__init__ 方法。当你覆盖 __init__ 时,这样做

class YourModel(models.Model):
    def __init__(self, *args, **kwargs):
        ...
        super(YourModel, self).__init__(*args, **kwargs)
        ...

__init__(self): 格式只接受一个参数self

【讨论】:

是的,我正在覆盖它,但不需要覆盖它,因此将其删除,现在代码工作正常。谢谢

以上是关于从 db 获取数据时,django 中的简单循环出现奇怪错误的主要内容,如果未能解决你的问题,请参考以下文章

渲染模板 django cms 插件中的简单循环

从 db 获取当前的 db 迁移状态,而不是 django 中的模型

如何计算年龄是不是在出生年份的范围内,同时从Django ORM中的Db获取出生年份

无法从 django 服务器中的数据库上传图像

从 api 获取时反应 useEffect 无限循环

如何从 django 模板中的 for 循环中获取特定的 id?