繁体   English   中英

django wsgi“没有模块命名模型”错误

[英]django wsgi “No module named models” error

我在使用 django + wsgi 时遇到了这个问题,我不明白为什么。 当我访问运行 apache2 + wsgi 的本地网站时,出现此错误:

ImportError at /
No module named models

但是当我使用开发服务器运行时,一切正常: python manage.py runserver 0:8080

我的 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()

我的模型.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)

我的 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 treeapt-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文件夹是项目级别文件夹而不是应用程序级别。 你真的不应该有项目级模型或视图。 创建一个应用程序并将您的模型和视图移动到其中。 见: https : //stackoverflow.com/a/2610797/884453

另外 b/c 它的reader/reader/models.py你可能在路径上有项目文件夹,所以reader.models不存在,但reader.reader.models我敢打赌。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM