django中的错误称为模块未找到

时间:2013-06-12 10:37:49

标签: python django

我创建了一个名为mysite的项目,我使用名为books的startapp创建。我创建了管理站点并且它成功运行。但是当我尝试将模型添加到我的管理站点时,发生了这些错误。

ImportError at /admin/
No module named books.models

实际上我在books文件夹中创建了admin.py,然后我编写了以下代码。

from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

models.py:

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __unicode__(self):
        return self.name

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title

那么,如何解决这个问题呢?

2 个答案:

答案 0 :(得分:0)

试试这个......

from django.contrib import admin
from models import Publisher, Author, Book

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

答案 1 :(得分:0)

我试试

from books.models import Publisher, Author, Book
...

如果图书是您应用的名称