0

我试图将我的数据库应用程序连接到 django 中的项目。成功连接 psycopg 和数据库。然后我开始为它构建应用程序

python manage.py startapp books

并在models.py中写了一些代码。

但是当我尝试将应用程序放入 INSTALLED_APPS 时,它会导致错误。

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'tut1.books',
# Uncomment the next line to enable the admin:
 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',

所以,我在项目的文件夹中并输入命令行:

%>python manage.py syncdb
ImportError: No module named 'tut1.books'

如果我从 INSTALLED_APPS 中删除我的应用程序,一切都会好起来的。我怀疑,链接应用程序必须有不同的语法。

试图将 'tut1.books' 更改为 'books'。导致另一个错误。

%>python manage.py syncdb
AttributeError:'module' object has no attribute 'URLFIELD'

项目 /tut1/ 中的文件和文件夹

   manage.py
   views.py
   /tut1/
   /templates/
   /books/

/books/ 中的文件

   __init__.py
   modules.py
   views.py
   tests.py

/tut1/tut1/ 中的文件

   __init__.py
   settings.py
   urls.py
   views.py
   wsgi.py

init .py 默认为空。

检查了这个项目的 PYTHONPATH。没什么奇怪的...

>>> import sys
>>> for i in sys.path:
      print(i)


C:\study\django\tut1
C:\Python33\Lib\idlelib
C:\Python33\lib\site-packages\setuptools-1.1.6-py3.3.egg
C:\Python33\python33.zip
C:\Python33\DLLs
C:\Python33\lib
C:\Python33
C:\Python33\lib\site-packages
4

2 回答 2

1

这可能是models.py 的问题。您似乎编写代码如下。

foo = models.URLFIELD()

“模型”模块没有 URLFIELD()。

您应该将其更改为 URLField()。

于 2013-10-03T08:44:31.833 回答
0

您需要__init__.py先将文件(可以为空)添加到文件夹 tut1.books,然后 python 才能导入它。看到这个答案:什么是 __init__.py?

于 2013-10-03T08:24:55.220 回答