Django - Method Not Allowed on Function Based View

2019-07-09 11:58发布

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条回答
小情绪 Triste *
2楼-- · 2019-07-09 12:36

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'),
]
查看更多
登录 后发表回答