Django-基于函数的视图(FBV)上不允许使用方法 [英] Django - Method Not Allowed on Function Based View (FBV)

查看:83
本文介绍了Django-基于函数的视图(FBV)上不允许使用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过AJAX调用提交POST请求时,得到了 405方法不允许响应:

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

我的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"),
]

A在我的模板中,我正在使用 x可编辑内联编辑来提交请求:

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>

考虑到,该请求似乎正在通过,并且由于CSRF而未被拒绝href = https://docs.djangoproject.com/en/1.10/ref/csrf/#rejected-requests rel = nofollow noreferrer>我没有得到403 ,而是得到405:

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?

推荐答案

我违反了一条基本规则。解决方法如下:

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

这篇关于Django-基于函数的视图(FBV)上不允许使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆