【问题标题】:django wsgi "No module named models" errordjango wsgi“没有名为模型的模块”错误
【发布时间】:2019-12-11 22:08:46
【问题描述】:

我在使用 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

【问题讨论】:

标签: python django wsgi


【解决方案1】:

确保在您项目的应用阅读器中有一个 __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 我敢打赌。

【讨论】:

  • 是的,我有__init__.py,这是默认的,我的意思是它是空的。我需要覆盖它吗?
  • 没有,只要它在那里,你的python代码应该能够看到models.py...你看到__init__.pycmodels.pyc文件吗?这两个文件表明您的 python 代码能够查看/解释阅读器应用程序的内容。
  • 现在没有.pyc 文件。我已经编辑了我的问题以添加 tree 结果。
猜你喜欢
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 2015-08-06
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多