简体   繁体   English

'Tag'对象没有属性'count'

[英]'Tag' object has no attribute 'count'

I keep getting this error when I go to the development server that the 'Tag' object has no attribute 'count'. 当我转到开发服务器时,“Tag”对象没有属性“count”时,我一直收到此错误。 I don't understand why the error comes up in line 117 when tag.count didn't generate any errors when used in the previous lines of code? 我不明白为什么当tag.count在前面的代码行中使用时没有产生任何错误时,第117行出现错误? Thanks! 谢谢!

Here's the error message: 这是错误消息:

AttributeError at /tag/
'Tag' object has no attribute 'count'
Request Method: GET
Request URL:    
http://127.0.0.1:8000/tag/

Django Version: 1.4
Exception Type: AttributeError
Exception Value:    
'Tag' object has no attribute 'count'
Exception Location: /Users/jonathanschen/Python/projects/skeleton/django_bookmarks/django_bookmarks/bookmarks/views.py in tag_cloud_page, line 117
Python Executable:  /usr/bin/python
Python Version: 2.7.1
Python Path:    
['/Users/jonathanschen/Python/projects/skeleton/django_bookmarks',
 '/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/distribute-0.6.27-py2.7.egg',
 '/Library/Python/2.7/site-packages/nose-1.1.2-py2.7.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages',
 '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time:    Mon, 9 Jul 2012 11:35:33 -0500

The code it refers back to is this: 它引用的代码是这样的:

def tag_cloud_page(request):
    MAX_WEIGHT = 5
    tags = Tag.objects.order_by('name')
    # Calculate tag min and max counts
    min_count = max_count = tags[0].bookmarks.count()
    for tag in tags:
        tag.count = tag.bookmarks.count()
        if tag.count < min_count:
            min_count = tag.count
        if max_count < tag.count:
            max_count = tag.count
        #calculate count range. Avoid dividing by zero.
        range = float(max_count - min_count)
        if range == 0.0:
            range = 1.0
        # Calculate tag weights.
        for tag in tags:
            tag.weight = int(
                MAX_WEIGHT * (tag.count - min_count) / range #line 117
            )
        variables = RequestContext(request, {
            'tags': tags
        })
        return render_to_response('tag_cloud_page.html', variables)

You iterate over tags twice with the same keyword tag . 您使用相同的关键字tag迭代tags两次。 Turn your second for loop into something like this: 把你的第二个for循环变成这样的东西:

for related_tag in tags:

Also, you need to change tag.weight = ... in that second loop, so that it refers to the correct tag and related_tag instances. 此外,您需要在第二个循环中更改tag.weight = ... ,以便它引用正确的tagrelated_tag实例。

You do not say which line is line 117. I am assuming it is the line: 你没有说哪一行是第117行。我假设它是行:

MAX_WEIGHT * (tag.count - min_count) / range

You are using the same iterator with the same name 'tag' within the for loop that also iterated using tag. 您在for循环中使用具有相同名称'tag'的相同迭代器,该迭代也使用tag进行迭代。 That will not work. 那不管用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM