python Django:导入错误:无法导入名称计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1469614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django: ImportError: cannot import name Count
提问by wenbert
I just pulled from my github and tried to setup my application on my Ubuntu (I originally ran my app on a Mac at home).
我刚刚从我的 github 中提取并尝试在我的 Ubuntu 上设置我的应用程序(我最初在家里的 Mac 上运行我的应用程序)。
I re-created the database and reconfigured the settings.py -- also update the template locations, etc.
我重新创建了数据库并重新配置了 settings.py——还更新了模板位置等。
However, when I run the server "python manage.py runserver" get an error that says:
但是,当我运行服务器“python manage.py runserver”时,收到一条错误消息:
ImportError: cannot import name Count
I imported the Count in my views.py to use the annotate():
我在 views.py 中导入了 Count 以使用 annotate():
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.db.models import Count
from mysite.blog.models import Blog
from mysite.blog.models import Comment
from mysite.blog.forms import CommentForm
def index(request):
#below, I used annotate()
blog_posts = Blog.objects.all().annotate(Count('comment')).order_by('-pub_date')[:5]
return render_to_response('blog/index.html',
{'blog_posts': blog_posts})
Why is not working?
为什么不工作?
Also, if I remove the "import Count" line, the error goes away and my app functions like normal.
此外,如果我删除“import Count”行,错误就会消失,我的应用程序运行正常。
Thanks, Wenbert
谢谢,温伯特
UPDATE:
更新:
my models.py looks like this:
我的models.py 看起来像这样:
from django.db import models
class Blog(models.Model):
author = models.CharField(max_length=200)
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.content
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
class Comment(models.Model):
blog = models.ForeignKey(Blog)
author = models.CharField(max_length=200)
comment = models.TextField()
url = models.URLField()
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.comment
UPDATE 2
更新 2
My urls.py looks like this:
我的 urls.py 看起来像这样:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^blog/$','mysite.blog.views.index'),
(r'^display_meta/$','mysite.blog.views.display_meta'),
(r'^blog/post/(?P<blog_id>\d+)/$','mysite.blog.views.post'),
)
采纳答案by David
This sounds like you're not using Django 1.1. Double check by opening up the Django shell and running
这听起来像是您没有使用 Django 1.1。通过打开 Django shell 并运行来仔细检查
import django
print django.VERSION
You should see something like (1, 1, 0, 'final', 0)
if you're using 1.1
(1, 1, 0, 'final', 0)
如果您使用的是 1.1,您应该会看到类似的内容
回答by freiksenet
I've updated my Django and it turns out that your import statement is correct as module structure was changed a bit. Are you sure your Django is of latest version?
我已经更新了我的 Django,结果证明你的 import 语句是正确的,因为模块结构有所改变。你确定你的 Django 是最新版本吗?