Search code examples
djangodjango-modelsdjango-formsdjango-viewsrating

Does adding a class to models.py ever cause "view does not exist" error?


I was trying to implement a rating system that receives the information that a user submits. But I was just wondering if it's possible to have two classes in one models file and get Could not import myapp.comments.views.stars. View does not exist in module myapp.comments.views.

In my models file, I have

class CommentWithRating(Comment):
    rating = models.IntegerField()

    def save(self, *args, **kwargs):
        self.content_object.rating.add(score=self.rating, user=self.user, ip_address=self.ip_address)
        super(CommentWithRating, self).save(*args, **kwargs)

class Rating(models.Model):
    first_name = models.charField(maxlength=30)
    last_name = models.charField(maxlength=30)
    department = models.charField(maxlength=30)
    comment = models.charField(maxlength=10000)
    communi_rating = models.IntegerField()
    prepar_rating = models.IntegerField()
    interact_rating = models.IntegerField()
    help_rating = models.IntegerField()

By the way, stars is a html file. Any ideas?

This is my views,

from django.shortcuts import render_to_response, render
from django.http import HttpResponse
from models import CommentWithRating
def stars(request):
    return render(request, 'star.html', {'score': ''})

My error message is simply,

Could not import myapp.comments.views.stars. View does not exist in module myapp.comments.views.

My traceback is,

Environment: Request Method: GET

Django Version: 1.4

Python Version: 2.7.2

Installed Applications:

('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'registration', 'django.contrib.admin', 'djangoratings') Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware')

Traceback: File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 101. request.path_info) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve 300. sub_match = pattern.resolve(new_path) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve 209. return ResolverMatch(self.callback, args, kwargs, self.name) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in callback 216. self._callback = get_callable(self._callback_str) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py" in wrapper 27. result = func(*args) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in get_callable 101. (lookup_view, mod_name))

Exception Type: ViewDoesNotExist at /rating/ Exception Value: Could not import myapp.comments.views.stars. View does not exist in module >myapp.comments.views.`


Solution

  • Yeah, that's definitely possible.

    Try doing a

    python ./manage.py shell
    

    and then importing the model or view that is giving you the problem. That might end up giving you more useful debugging information.

    (from Django views does not exist or could not import)