python - 使用 django 的 heroku 不允许使用 405 POST 方法

标签 python django web-services heroku

我有一个 django 网络服务,它在本地运行得非常好,但是当我将它上传到 heroku 时,无论我在哪里发帖,我都会在尝试发帖时收到 405 错误。 我已将 csrf_exempt 添加到我的所有帖 subview 中。这些是基于类的 View 。 例如:

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

关于python - 使用 django 的 heroku 不允许使用 405 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23567814/

相关文章:

python - sphinx 删除自动模块中 args 的模块前缀

python - django 管理应用程序错误(带有属性字段的模型): global name 'full_name' is not defined

python - Swig 从 Base* 向下转型到 Derived*

python - Django:在admin list_display中显示相关对象的数量

css - Django 1.9.7 管理页面 - CSS 已加载但未呈现

java - 如何在 jax-ws 客户端中隐藏警告(可能)由 jax-ws 库引起

java - WebService java 中的哈希表更新

sql-server - SSIS WebService 任务插入 XML header

python - openCV 阈值负值

python - Python 中等效的 'nth_element' 函数是什么?