Django - Method Not Allowed on Function Based View (FBV)

跟風遠走 提交于 2019-12-11 00:56:11

问题


I am getting a 405 METHOD NOT ALLOWED response when I am trying to submit a POST request through an AJAX call:

"POST /events/profile_update/ HTTP/1.1" 405 0

I'm trying to set this up with the most basic view:

def profile_update(request):

    if request.method == "POST":
        name_form =forms.EventName(request.POST)

        if name_form.is_valid():
            name = name_form.cleaned_data['name']
    else:
        name_form = forms.EventName()

    return render(request, 'event_edit_profile.html', {"name": name})

my urls.py:

urlpatterns = [
    url(r'^(?P<slug>[-\w]+)/update/$', views.EventProfileUpdateView.as_view(), name='event_profile_update'),
    url(r'^profile_update/$', views.profile_update, name="profile_update"),
]

And in my template, I am using x-editable inline editing to submit the request:

<h1 id="name" data-type="text" data-pk="{{ object.id }}" data-url="{% url 'Events:profile_update' %}" data-title="Event Name" data-params="{csrfmiddlewaretoken:'{{csrf_token}}'}">{{ object.name }}</h1>

The request seems to be coming through, and is not being rejected due to CSRF, given that I don't get a 403, but rather a 405:

"POST /events/profile_update/ HTTP/1.1" 405 0

For some reason, I can't seem to get past this. Anyone have any ideas as to what could be screwing me up?


回答1:


I broke a basic rule. Here's the fix:

urlpatterns = [
    url(r'^profile_update/$', views.profile_update, name="profile_update"),
    url(r'^(?P<slug>[-\w]+)/update/$', views.EventProfileUpdateView.as_view(), name='event_profile_update'),
]


来源:https://stackoverflow.com/questions/40800061/django-method-not-allowed-on-function-based-view-fbv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!