Django: ImportError: cannot import name Count

I just pulled it out of my github and tried to configure the application on my Ubuntu (I initially ran my application on a Mac at home).

I re-created the database and reconfigured settings.py - also updated the location of the templates, etc.

However, when I start the server "python manage.py runningerver", we get the error message:

ImportError: cannot import name Count

I imported Count into my views.py to use 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 does not it work?

Also, if I delete the โ€œCount Countโ€ line, the error disappears and my application functions as usual.

Thanks, Wenbert

UPDATE:

my models.py looks like this:

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

My urls.py looks like this:

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'),
)
+3
source share
2

, Django 1.1. , Django

import django
print django.VERSION

- (1, 1, 0, 'final', 0), 1.1

+1

Django, , import , . , Django ?

+1

Source: https://habr.com/ru/post/1718433/


All Articles