2012-07-31 63 views
0

我收到以下錯誤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中添加了評分應用。

+0

函數應該以小寫字母http://www.python.org/dev/peps/pep-0008/#function-names開頭。類必須是http://www.lcthon.org/dev/peps/pep-0008/#class-names。這是收視率/ views.py? – Willian 2012-07-31 13:33:05

+0

例外位置:\t get_callable,第101行 – Caffeinatedwolf 2012-07-31 13:51:12

+0

和@Willian C:\ Python27 \ lib \ site-packages \ django \ core \ urlresolvers.py yes是ratings/views.py文件 – Caffeinatedwolf 2012-07-31 13:51:39

回答

2

您可能有一個循環導入。在你的def中移動你的import語句,看看是否有幫助。

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

我已經嘗試過,但它說「在模塊ratings.views中不存在視圖」。 – Caffeinatedwolf 2012-07-31 13:29:56

1

您是否剝離了ratings/views.py直到最簡單的視圖文件它可能是?如果你能以非常簡單的視角來工作,那就從現在開始到現在你的工作。

另外,請考慮您的命名功能(HotelRating將成爲hotel_rating)和類時下列PEP8。這會讓您與代碼共享的任何人都更輕鬆,包括本網站!

+0

是的,我甚至用一個簡單的「通行證」嘗試過,但它找不到視圖。 – Caffeinatedwolf 2012-07-31 13:52:55