我有这个问题与django + wsgi,我不明白为什么。当我去我的本地网站运行apache2 + wsgi,我有这个错误:
ImportError at /
No module named models
但是当我运行开发服务器时,一切都很好:python manage.py runserver 0:8080
.
My wsgi.py:
import os, sys
sys.path.append('/var/www/reader')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "reader.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
我models.py from django.db import models
from django.contrib.auth.models import User
class Feed(models.Model):
display_name = models.CharField(max_length=255)
link_feed = models.CharField(max_length=255)
user = models.ForeignKey(User)
My views.py抛出错误的地方:
from django.shortcuts import render
from reader.models import Feed
def index(request):
feeds_list = Feed.objects.all()
context = {'feeds_list': feeds_list}
return render(request, 'home/index.html', context)
在我的设置中:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'reader', <-- this is my app
)
我在Python 2.7.4和django 1.5.1下运行。
谁能给我解释一下为什么?
更新1:
jeremy@dev:/var/www/reader$ tree .
.
├── manage.py
└── reader
├── __init__.py
├── models.py
├── reader.settings
├── settings.py
├── static
│ ├── images
│ ├── scripts
│ │ └── script.js
│ └── styles
│ └── style.css
├── templates
│ ├── base.html
│ ├── home
│ │ └── index.html
│ └── subscription
│ └── add.html
├── urls.py
├── views.py
└── wsgi.py
8 directories, 13 files
确保在项目的应用程序阅读器中有一个__init__.py
文件
project/
-- reader/
---- __init__.py
---- models.py
---- views.py
调试帮助
是否安装了tree
?
如果不是brew install tree
或apt-get install tree
(根据您的开发环境进行更改)
cd ~/path/to/djangoproject
tree .
,然后将输出粘贴到上面,这样我们可以更好地了解发生了什么。
francis at Kraken.local ~/Sites/yaconiello/blog on francis*
$ tree .
.
├── __init__.py
├── __init__.pyc
├── admin
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── article.py
│ ├── article.pyc
│ ├── category.py
│ └── category.pyc
├── feeds
│ ├── __init__.py
│ ├── category.py
│ └── latest.py
├── migrations
│ ├── 0001_initial.py
│ ├── 0001_initial.pyc
│ ├── 0002_auto__add_field_article_excerpt.py
│ ├── 0002_auto__add_field_article_excerpt.pyc
│ ├── __init__.py
│ └── __init__.pyc
├── models
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── article.py
│ ├── article.pyc
│ ├── category.py
│ ├── category.pyc
│ ├── request.py
│ └── vote.py
├── signals.py
├── signals.pyc
├── templates
│ └── blog
│ ├── article
│ │ ├── category_archive.html
│ │ ├── date_archive.html
│ │ ├── index.html
│ │ └── single.html
│ ├── base.html
│ ├── emails
│ │ └── new_comment.html
│ ├── feeds
│ │ └── description.html
│ └── snippets
│ └── article_list.html
├── templatetags
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── blogs.py
│ └── blogs.pyc
├── urls.py
├── urls.pyc
└── views
├── __init__.py
├── __init__.pyc
├── article.py
└── article.pyc
12 directories, 45 files
更新你的项目是reader。这样,reader
文件夹是项目级文件夹,而不是应用级文件夹。你真的不应该有项目级别的模型或视图。创建一个应用程序,并将你的模型和视图移到其中。参见:https://stackoverflow.com/a/2610797/884453
另外b/c它的reader/reader/models.py
,你可能有项目文件夹在路径上,所以reader.models
不存在,但reader.reader.models
我打赌有。