簡體   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