我已经阅读了SO上的所有内容以及所有django文档都在寻找解决方案。我的模型工作得非常好但是我不能将它们导入到我的'getnews.py'文件和models.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'等。, 按照其他Feed中的建议手动添加系统路径到系统路径请帮助!!
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)
添加了这个,因为SO上的大多数其他帖子指向设置DJANGO_SETTINGS_MODULE必须手动设置bur,因为你已经可以看到它
答案 0 :(得分:1)
鉴于目录结构,您的导入应该正常工作。
如何尝试相对导入呢?在views.py
和getnews.py
这个应该工作:
from models import ...
答案 1 :(得分:0)
from .models import newsurls, newsblock
另请阅读PEP8 http://legacy.python.org/dev/peps/pep-0008/因为您的代码格式不正确。
编辑:我看到你试图这样做,但没有'。'标志。请注意,在型号之前是'。'像那样.models