【问题标题】:405 POST method no allowed on heroku with django405 POST 方法在 django 的 heroku 上不允许
【发布时间】:2014-06-27 09:10:59
【问题描述】:

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

)

没有帖子在本地工作。

【问题讨论】:

  • 请发布您的相关网址配置,以及您尝试发布到哪个网址。
  • @YuvalAdam 添加了 urls.py

标签: python django web-services heroku


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2019-07-05
    • 2014-05-23
    • 2021-04-16
    • 2018-12-20
    • 2015-11-16
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多