Django查看错误

时间:2012-07-31 12:57:42

标签: python django

我收到以下错误

Django Version:     1.4
Exception Type:     ViewDoesNotExist
Exception Value:    

Could not import ratings.views.HotelRating. View does not exist in module ratings.views.

但这是我的views.py

from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from ratings.models import Hotel_Rating, Restaurant_Rating, Cafe_Rating, Pastry_Rating
from services.models import *
from ratings.forms import Hotel_Rating_From
from django.template import RequestContext

def HotelRating(request):
    if request.method == 'POST':
        form = Hotel_Rating_From(request.POST)
        if form.is_valid():
            user_id = form.cleaned_data['user_id']
            hotel_id = form.cleaned_data['hotel_id']
            user = User.objects.get(id = user_id)
            hotel = Hotel.objects.get(id = hotel_id)
            review = Hotel_Rating(hotel_id = hotel.id, user_id = user.id, overall_rating = form.cleaned_data['overall_rating'], service = form.cleaned_data['service'], cleanliness = form.cleaned_data['cleanliness'], location = form.cleaned_data['location'], rooms = form.cleaned_data['rooms'], restaurant = form.cleaned_data['restaurant'], room_service = form.cleaned_data['room_service'], price = form.cleaned['price'], comment = form.cleaned_data['comment'])
            review.save()
            return HttpResponseRedirect('/')
        else:
            form = Hotel_Rating_From(request.POST)
            return render_to_response('hotelreview.html', {'form': form}, context_instance = RequestContext(request))

这是我的urls.py文件

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),
    (r'^register/$', 'reviewers.views.ReviewerRegistration'),
    (r'^hotelrating/$', 'ratings.views.HotelRating'),
    (r'^login/$', 'reviewers.views.LoginRequest'),

)

我不知道我做错了什么,我在settings.py的INSTALLED_APPS中添加了评分应用。

2 个答案:

答案 0 :(得分:2)

你可能有一个循环导入。在def中移动你的import语句,看看是否有帮助。

def HotelRating(request):
    from ratings.models import Hotel_Rating

答案 1 :(得分:1)

您是否已将ratings/views.py剥离到最简单的视图文件?如果你能用一个非常简单的视图来处理它,那就从那里开始工作到你现在拥有的东西。

另外,在命名函数(HotelRating将成为hotel_rating)和类时,请考虑关注PEP8。它将使您与代码共享代码的任何人都更容易,包括这个网站!