繁体   English   中英

使用django时,不允许使用405 POST方法

[英]405 POST method no allowed on heroku with django

我有一个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'),

)

非帖子非本地工作。

我不知道这是否是您错误的确切原因,但在实例方法上使用装饰器时,您必须将其包装在@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM