1

我从 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 是空列表,所以我想知道我实际上做错了什么?

提前谢谢你们。

4

1 回答 1

0

似乎你已经覆盖了or的__init__方法。当你覆盖 时,这样做CityJob__init__

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

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

于 2012-08-19T10:28:59.807 回答