我从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_URL}}images/post_btn.gif" border="none" alt=""><br><img src="{{STATIC_URL}}images/ffree_text.gif"></a></div><!--post_btn-->
89 : <div class="create_prfoile_btn"><a href="#"><img src="{{STATIC_URL}}images/banner.gif" alt="" 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是空列表,所以我想知道我实际上做错了什么?
先谢谢你们。
答案 0 :(得分:0)
似乎您已覆盖__init__
或City
的{{1}}方法。当您覆盖Job
时,请执行
__init__
格式class YourModel(models.Model):
def __init__(self, *args, **kwargs):
...
super(YourModel, self).__init__(*args, **kwargs)
...
只接受一个参数__init__(self):
。