Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django import error: No module named models

I keep receiving the error Could not import movies.views. Error was: No module named models

Here is my traceback:

    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

I am not sure why I have this error. My code is as follows...

I have an django app called tracking and another called movies. I have a python file called tracking.py in the tracking app it consists of the following code:

filmlibrary/tracking/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()

The above is pretty basic stuff. My views.py in my movies app uses the tracking.py file here:

filmlibrary/movies/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

UPDATE:

Here are the contents of 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)

The error appears to come from the import line from tracking.models import MovieView in the tracking.py file and I am not sure why. As soon as I comment out that line the error goes away but then of course I'll have new errors about MovieView not existing as expected. I don't see anything wrong with that import line in tracking.py.

Does anyone have any suggestions as to why I receive that error and how I can resolve this?

like image 613
tdelam Avatar asked Nov 06 '12 19:11

tdelam


1 Answers

Can you try

from models import MovieView

instead of

from tracking.models import MovieView

The reason being both models.py and tracking.py are in the same app folder "tracking", you don't need to write tracking.models, this will make Django think that you have a folder named models inside the tracking directory.

like image 196
Hari Palappetty Avatar answered Sep 28 '22 08:09

Hari Palappetty