0

我收到以下错误

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 中添加了评分应用程序。

4

2 回答 2

2

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

def HotelRating(request):
    from ratings.models import Hotel_Rating
于 2012-07-31T13:11:18.183 回答
1

您是否已精简ratings/views.py到可能是最简单的视图文件?如果你能用一个非常简单的视图让它工作,那么从那里开始工作到你现在所拥有的。

此外,在命名您的函数(将成为)和类时,请考虑遵循PEP8 。它将使您与之共享代码的任何人(包括此站点)更容易!HotelRatinghotel_rating

于 2012-07-31T13:33:41.493 回答