向models.py添加一个类是否导致“视图不存在”错误?

时间:2012-08-09 04:15:32

标签: django django-models django-forms django-views rating

我正在尝试实施一个接收用户提交信息的评级系统。 但我只是想知道是否有可能在一个模型文件中有两个类并获得Could not import myapp.comments.views.stars. View does not exist in module myapp.comments.views.

在我的模型文件中,我有

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()

顺便说一下,星星是一个html文件。有什么想法吗?

这是我的观点,

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': ''})

我的错误信息很简单,

  

无法导入myapp.comments.views.stars。模块中不存在视图   myapp.comments.views。

我的追溯是,

  

环境:   请求方法:GET

     

Django版本:1.4

     

Python版本:2.7.2

     

已安装的应用程序:

     

( 'django.contrib.auth',   'django.contrib.contenttypes',   'django.contrib.sessions',   'django.contrib.sites',   'django.contrib.messages',   'django.contrib.staticfiles',   '注册',   'django.contrib.admin',   'djangoratings')   已安装的中间件:   ( 'django.middleware.common.CommonMiddleware',   'django.contrib.sessions.middleware.SessionMiddleware',   'django.middleware.csrf.CsrfViewMiddleware',   'django.contrib.auth.middleware.AuthenticationMiddleware',   'django.contrib.messages.middleware.MessageMiddleware')

     

回溯:   在get_response中输入文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py”    101. request.path_info)   文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py”解析    300. sub_match = pattern.resolve(new_path)   文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py”解析    209.返回ResolverMatch(self.callback,args,kwargs,self.name)   回调文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py”    216. self._callback = get_callable(self._callback_str)   包装器中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py”    27. result = func(* args)   get_callable中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py”    101.(lookup_view,mod_name))

     

异常类型:ViewDoesNotExist at / rating /   例外值:无法导入myapp.comments.views.stars。模块> myapp.comments.views。

中不存在视图

1 个答案:

答案 0 :(得分:2)

是的,这绝对是可能的。

尝试做

python ./manage.py shell

然后导入提供问题的模型或视图。这最终可能会为您提供更有用的调试信息。

(来自Django views does not exist or could not import