使用django在heroku上不允许使用405 POST方法

时间:2014-05-09 14:57:08

标签: python django web-services heroku

我有一个django网络服务,在本地工作得非常好,但是一旦我上传到heroku,当我尝试发布时,无论我发布在哪里,我都会遇到405错误。 我在所有帖子视图中添加了一个csrf_exempt。这些是基于类的视图。 例如:

class ApplyForRental(View):
    def post(self, request, rentalID):
        #user = User.objects.filter(pk = rentalID)
        #filtered = Contentfile.objects.filter(file_owner = user, published=True)
        rental = RentProperty.objects.get(pk = rentalID)
        applicant = User.objects.get(pk=request.POST.get('interested_renter'))
        rental.interested_renters.add(applicant)

        jsonDict = {"success":True}
        data = json.dumps(jsonDict)

        return HttpResponse(data, content_type='application/json')

    @csrf_exempt
    def dispatch(self,*args,**kwargs):
        return super(ApplyForRental, self).dispatch(*args,**kwargs)

为什么它不能在heroku上工作但在本地工作的原因?

我的网址文件: 主

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'homerun.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^rentals/', include('rentals.urls', namespace="rentals")),
    url(r'^users/(?P<userID>\w+)/$', views.UserInfo.as_view(), name='getUser'),
    (r'^grappelli/', include('grappelli.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

应用

urlpatterns = patterns('',

    url(r'^create/$', views.CreateRental.as_view(), name='createRental'),
    url(r'^(?P<rentalID>\w+)/$', views.RentalInformation.as_view(), name='getrental'),
    url(r'^users/(?P<userID>\w+)/$', views.UserRentals.as_view(), name='userrentals'),
    url(r'^(?P<rentalID>\w+)/uploadimage/$', views.UploadImage.as_view(), name='uploadimage'),
    url(r'^(?P<rentalID>\w+)/apply/$', views.ApplyForRental.as_view(), name='applyforrental'),
    url(r'^$', views.RentalsList.as_view(), name='getRentals'),


    #url(r'^filesInfoByOwner/(?P<userName>\w+)/pk/(?P<pk>\d+)/$', views.FileInfo.as_view(), name='filesByOwnerAndPK'),
    #url(r'^filesContentByOwner/(?P<userName>\w+)/pk/(?P<pk>\d+)/$', views.GetFileContent.as_view(), name='fileContent'),

)

非帖子非本地工作。

1 个答案:

答案 0 :(得分:0)

我不知道这是否是导致错误的确切原因,但在实例方法上使用装饰器时,必须将其包装在@method_decorator调用中。所以你的调度函数应该是这样的:

from django.utils.decorators import method_decorator

@method_decorator(csrf_exempt)
def dispatch(self,*args,**kwargs):
    return super(ApplyForRental, self).dispatch(*args,**kwargs)

https://docs.djangoproject.com/en/1.7/topics/class-based-views/intro/#decorating-the-class