我希望能在这个问题上寻求一些帮助。我还在学习Django(和Python)并遇到这个我无法找到答案的特殊问题。我创建了一个名为“新闻”的新应用程序,并为应用程序设置了模型。使用Admin界面我创建了一些数据。在我的“Pages”应用程序中,我正在尝试导入News_Article
类并获取错误No module News.models
。
我很难看到这里出了什么问题。
非常感谢任何协助。
DIR结构
Bolton_GC [Folder]
- Bolton_GC [Folder]
- News [Folder]
- Migrations [Folder]
- __init__.py
- __init__.pyc
- admin.py
- admin.pyc
- models.py
- models.pyc
- tests.py
- views.py
- Pages [Folder]
- Migrations [Folder]
- __init__.py
- __init__.pyc
- admin.py
- admin.pyc
- models.py
- models.pyc
- tests.py
- views.py
- views.pyc
- static [Folder]
- templates [Folder]
- __init__.py
- __init__.pyc
- settings.py
- settings.pyc
- urls.py
- urls.pyc
- wsgi.py
- wsgi.pyc
- db.sqlite3
- manage.py
消息\ model.py
from django.db import models
from datetime import datetime
class News_Article(models.Model):
class Meta:
ordering = ['news_datetime_submitted']
news_title = models.CharField(max_length=75, verbose_name="News Title")
news_text = models.CharField(max_length=300, verbose_name="News Text")
news_active = models.BooleanField(default=True, verbose_name="News Active")
news_datetime_submitted = models.DateTimeField(default=datetime.now(), verbose_name="News Date")
def __str__(self):
return self.news_title
页面\ views.py
from django.shortcuts import HttpResponse, get_object_or_404, render
from models import Page, Announcement, Menu, Sub_Menu
from django.core.exceptions import ObjectDoesNotExist
from News.models import News_Article
import pdb
# Helper Functions
def get_announcement():
try:
return Announcement.objects.get(announcement_active=True)
except ObjectDoesNotExist:
return None
def clean_url(dirtyurl, badlist):
for item in badlist:
dirtyurl = dirtyurl.replace(item,'')
return dirtyurl[1:-1]
# View functions
def page(request):
rDict = {}
path = clean_url(request.path, ['"', "'"])
# pdb.set_trace()
p = get_object_or_404(Page, urlconf_text=path)
rDict['p'] = p
announcement = get_announcement()
if not announcement == None:
rDict['announcement'] = announcement
rDict['sitenav'] = path
rDict['menu'] = Menu.objects.all().order_by('menu_position')
return render(request, 'en/public/page.html', rDict)
错误
ImportError at /home/
No module named News.models
Request Method: GET
Request URL: http://127.0.0.1:8000/home/
Django Version: 1.8.2
Exception Type: ImportError
Exception Value:
No module named News.models
Exception Location: C:\Me\Websites\Bolton_GC\Bolton_GC\Pages\views.py in <module>, line 4
Python Executable: c:\python27\python.exe
Python Version: 2.7.9
Python Path:
['C:\\Me\\Websites\\Bolton_GC',
'c:\\python27\\lib\\site-packages\\setuptools-18.0.1-py2.7.egg',
'C:\\WINDOWS\\SYSTEM32\\python27.zip',
'c:\\python27\\DLLs',
'c:\\python27\\lib',
'c:\\python27\\lib\\plat-win',
'c:\\python27\\lib\\lib-tk',
'c:\\python27',
'c:\\python27\\lib\\site-packages']
Server time: Tue, 14 Jul 2015 13:21:14 +0100
答案 0 :(得分:30)
切换
from News.models import News_Article
到
from Bolton_GC.News.models import News_Article
答案 1 :(得分:12)
只是详细说明@TheLifeOfSteve的答案,所有import语句都始终与manage.py文件相关。
如果manage.py文件位于Bolton_GC / Bolton_GC路径,那么正确的import语句就是:
from News.models import News_Article
但是在当前的目录结构中,以下是史蒂夫指出的正确答案。
from Bolton_GC.News.models import News_Article
答案 2 :(得分:0)
也有同样的问题,但对我而言,这是一次名称冲突。我已将我的应用命名为路径中的另一个模块。我重构了应用名称,就解决了问题。
答案 3 :(得分:0)
导入和注册模型有更新!! 请尝试从 .models 导入 News_Article
答案 4 :(得分:-2)
试试 从 .models 导入 News_Article