django:无法在与models同路径的.py文件中导入模型
我在StackOverflow和Django的文档上看了很多内容,想找到解决这个问题的方法。我的模型(models)运行得很好,但我无法在与models.py同一个目录下的'getnews.py'文件中导入它们。在views.py中可以正常导入,而且绝对没有循环导入的问题!
models.py:
from django.db import models
import datetime
from django.utils import timezone
class newsurls(models.Model):
title = models.CharField(max_length=200)
def __unicode__(self):
return unicode(self.title)
pub_date = models.DateTimeField("date published")
class newsblock(models.Model):
news = models.ForeignKey(newsurls)
url = models.URLField(max_length=2000)
def __unicode__(self):
return unicode(self.url)
image = models.URLField(max_length=2000)
favi = models.URLField(max_length=2000)
bgcolor = models.CharField(max_length=20)
tcolor = models.CharField(max_length=20)
genre = models.CharField(max_length=200)
在views.py中:(可以正常工作)
from news.models import newsurls, newsblock
getnews.py:(无法正常工作)
from news.models import newsurls, newsblock
错误追踪信息:
Traceback (most recent call last):
File "/home/skru/newsshuffle/news/getnews.py", line 3, in <module>
from news.models import newsurls, newsblock
ImportError: No module named news.models
views.py:
from django.shortcuts import render_to_response
from news.models import newsurls, newsblock
try:
import cPickle as pickle
except:
import pickle
import random
def news(request):
newsDatabase = open('/home/skru/newsshuffle/news/storyDb.p', 'r+')
openData = newsDatabase.read()
dstory = pickle.loads(openData)
count = dstory['count']['links']
story = []
outstorys = []
keys = dstory.keys()
for key in keys:
if key != 'count':
story.append(dstory[key]['genre'])
story.append(dstory[key]['title'])
story.append(dstory[key]['url'])
story.append(dstory[key]['image'])
story.append(dstory[key]['bgcolor'])
story.append(dstory[key]['via'])
story.append(dstory[key]['tcolor'])
outstorys.append(story)
story = []
random.shuffle(outstorys)
lists = newsurls.objects.order_by('-pub_date')[:100]
return render_to_response('news/news.html',
{
'story_list':outstorys,
'count':count,
'lists':lists,
})
文件路径:
├── db.sqlite3
├── manage.py
├── news
│ ├── static
│ │ └── news
│ │ ├── news.css
│ │ └── ...
│ ├── templates
│ │ └── news
│ │ ├── allnews.html
│ │ └── ...
│ ├── __init__.py
│ ├── admin.py
│ ├── dd.py
│ ├── getPrevDate.py
│ ├── getnews.py
│ ├── models.py
│ ├── storyDb.p
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
└── newsshuffle
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
我尝试了各种不同的导入方式,比如'newsshuffle.news.models'等等,还手动将系统路径添加到系统路径中,正如其他帖子建议的那样,请帮帮我!!
manage.py:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "newsshuffle.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
我添加这个是因为大多数其他帖子提到需要手动设置DJANGO_SETTINGS_MODULE,但正如你所看到的,它已经设置好了。
2 个回答
1
你试过这样做吗?
from .models import newsurls, newsblock
另外,请你也看看PEP8的内容,地址是 http://legacy.python.org/dev/peps/pep-0008/,因为你的代码格式不太对。
补充说明:我看到你在尝试这样做,但忘记加'.'符号了。请注意,在模型前面要加一个'.',像这样 .models。
1
根据你提供的文件夹结构,你的导入应该是可以正常工作的。
那试试用相对导入的方法怎么样?在 views.py
和 getnews.py
这两个文件里,这样做 应该 可以成功:
from models import ...