模块review.views中不存在视图

时间:2014-08-14 02:32:50

标签: python django

所以我在django中收到此错误

Exception Value:  Could not import reviews.views.get_user_review. View does not exist in module reviews.views.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in get_callable, line 118

这是我的urls.py

from django.conf.urls import patterns, include, url

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'feed.views.home'),
    url(r'', include('social_auth.urls')),
    url(r'^logged-in/$', 'custom_user.views.fb_loggedin'),

    #url(r'^review/get/(?P<userid>.+?)/$', 'reviews.views.get_user_reviews_json'),

    url(r'^rating/add/(?P<actor>.+?)/(?P<target>.+?)/(?P<category>.+?)/(?P<rating>.+?)/$', 'rating.views.add_rating'),
    url(r'^rating/get/(?P<userid>.+?)/$', 'rating.views.get_user_ratings'),     

)

urlpatterns += staticfiles_urlpatterns()

要删除错误,我已评论与评论应用相关的所有内容。但错误仍然存​​在。这是reviews.views

'''
from django.shortcuts import render
from models import Reviews
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.utils import simplejson
from django.core import serializers

def user_review(userid):
    user = User.objects.get(id = userid)
    reviews  = Reviews.objects.filter(subject_user=user).select_related('user')
    return reviews
    #HttpResponse(data, mimetype='application/javascript')

def get_user_reviews_json(userid):
    reviews = user_review(userid)
    data = serializers.serialize("json", reviews)
    return data
'''

编辑:

函数get_user_reviews_json之前被定义为get_user_review,我忘了将urls.py从get_user_review更新为get_user_reviews_json,从而得到了错误。但后来我更新了它仍然存在错误。然后我注意到你所看到的一切,但仍然是同样的错误。

1 个答案:

答案 0 :(得分:0)

您的视图应该使用请求对象来响应Web请求。所以你的功能应该如下所示:

def get_user_reviews_json(request, userid):
    reviews = user_review(userid)
    data = serializers.serialize("json", reviews)
    return Response(data)  #from rest_framework.response import Response

同样,每个视图函数都将HttpRequest对象作为其第一个参数,通常将其命名为request.I希望它有所帮助。