简体   繁体   中英

405 POST method no allowed on heroku with django

I have a django web service which works perfectly fine locally, but as soon as I upload it to heroku, I keep getting 405 error when I try to post, no matter where I post. I have added a csrf_exempt to all my post view. these are class based view. for example:

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)

any reason why it wouldn't work on heroku but would work locally?

my urls files: main

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)),
)

app

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'),

)

Non of the posts work non locally.

I don't know if this is the exact cause of your error, but when using a decorator on an instance method, you have to wrap it in the @method_decorator call. So your dispatch function should look like this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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