我一直收到错误Could not import movies.views. Error was: No module named models
这是我的追溯:
Environment:
Request Method: GET
Request URL: http://localhost:8000/movies/awesome-movie/
Django Version: 1.3.1
Python Version: 2.7.3
Installed Applications:
['django.contrib.auth',
'username_patch',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.flatpages',
'south',
'tagging',
'tagging_autocomplete',
'accounts',
'movies',
'tracking',
'djcelery',
'pagination']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'pagination.middleware.PaginationMiddleware')
Traceback:
File "/Users/jeff/Code/filmlibrary/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/Users/jeff/Code/filmlibrary/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/Users/jeff/Code/filmlibrary/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/Users/jeff/Code/filmlibrary/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
158. return ResolverMatch(self.callback, args, kwargs, self.name)
File "/Users/jeff/Code/filmlibrary/lib/python2.7/site-packages/django/core/urlresolvers.py" in _get_callback
167. raise ViewDoesNotExist("Could not import %s. Error was: %s" % (mod_name, str(e)))
Exception Type: ViewDoesNotExist at /movies/awesome-movie/
Exception Value: Could not import movies.views. Error was: No module named models
我不知道为什么会出现这个错误。我的代码如下......
我有一个名为tracking
的django应用程序和另一个名为movies
的应用程序。我在tracking.py
应用程序中有一个名为tracking
的python文件,它包含以下代码:
filmlibrary /跟踪/ tracking.py
from movies.models import Movie
from tracking.models import MovieView
import os
import base64
def tracking_id(request):
try:
return request.session['tracking_id']
except KeyError:
request.session['tracking_id'] = base64.b64encode(os.urandom(36))
return request.session['tracking_id']
def log_movie_view(request, movie):
t_id = tracking_id(request)
try:
v = MovieView.objects.get(tracking_id=t_id, movie=movie)
v.save()
except MovieView.DoesNotExist:
v = MovieView()
v.movie = movie
v.ip_address = request.META.get('REMOTE_ADDR')
v.tracking_id = t_id
v.user = None
if request.user.is_authenticated():
v.user = request.user
v.save()
以上是非常基本的东西。我的views.py
应用中的movies
使用了track.py文件:
filmlibrary /电影/ views.py
@login_required
def movie_details(request, slug, template_name="movies/movie_detail.html"):
movie = get_object_or_404(Movie, slug=slug)
movie.tracking_id = tracking.tracking_id(request)
movie.save()
tracking.log_movie_view(request, movie)
context = RequestContext(request, {'movie': movie })
if movie:
try:
screener = movie.moviemedia_set.get(movie_type='screener')
.... continued
更新
以下是filmlibrary / tracking / models.py
的内容from django.db import models
from django.contrib.auth.models import User
from movies.models import Movie
class PageView(models.Model):
class Meta:
abstract = True
date = models.DateTimeField(auto_now=True)
ip_address = models.IPAddressField()
user = models.ForeignKey(User, null=True)
tracking_id = models.CharField(max_length=50, default='')
class MovieView(PageView):
movie = models.ForeignKey(Movie)
错误似乎来自tracking.py文件中的导入行from tracking.models import MovieView
,我不确定原因。一旦我注释掉该行,错误就会消失,但当然我会有关于MovieView的新错误没有按预期存在。我没有在tracking.py中看到该导入行有任何问题。
有没有人对我收到错误的原因以及如何解决此问题有任何建议?
答案 0 :(得分:17)
你能试试吗
来自模型的导入MovieView
而不是
来自tracking.models的导入MovieView
因为models.py和tracking.py都在同一个app文件夹中“跟踪”,你不需要编写tracking.models,这会让Django认为你在跟踪中有一个名为models的文件夹。目录
答案 1 :(得分:11)
虽然在某些情况下这是一个很好的解决方案,但此错误也可能是因为应用名称与内置应用的冲突造成的。
我知道我们的团队遇到了这个错误,这是因为实际的应用名称。这一点也很重要,因为如果是这种情况,您将无法将模型引入其他应用程序。
#You cannot use this format out of scope
from models import Test
#You can use this both in and out of scope in Django
from myapp.models import Test
如果第二个示例在您的Django项目中不起作用,则可能使用了冲突的应用名称。无法使用的内置应用程序的良好示例包括(但不限于)“测试”和“管理员”。
我会提醒您不要使用绑定修补程序并密切调查您的应用名称。