tags:

views:

1518

answers:

3

Hello,

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).

I re-created the database and reconfigured the settings.py -- also update the template locations, etc.

However, when I run the server "python manage.py runserver" get an error that says:

ImportError: cannot import name Count

I imported the Count in my views.py to use the 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.

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'),
)
+1  A: 

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?

freiksenet
It is not working for me. I get this error:Could not import mysite.blog.views. Error was: No module named aggregates
wenbert
Could you post your urls.py too?
freiksenet
freiksenet, I added urls.py in my question above
wenbert
+1  A: 

This sounds like you're not using Django 1.1. Double check by opening up the Django shell and running

import django
print django.VERSION

You should see something like (1, 1, 0, 'final', 0) if you're using 1.1

David
You are right. I get this: (1, 0, 2, 'final', 0). I installed Django using the Synaptic Package Manager in Ubuntu. I will try to uninstall and install the one from the Django website.
wenbert
It worked. Thanks so much for the answer.
wenbert
A: 

I also had that error, just delete the *. pyc and all will be in order

eos87