http响应状态405使用jquery提交时不允许的方法

时间:2013-12-17 16:25:27

标签: jquery python django

我尝试使用jquery-ui对话框小部件执行提交以进行更新,但http响应状态为405 METHOD NOT ALLOWED。

我的实施是否缺乏?

表单

ItemPriceFormset = inlineformset_factory(
    Item,
    ItemPrice,
    form = ItemPriceForm,
    can_delete=False,
    extra = 1)

浏览

class ItemDetailView(DetailView):
    model = Item
    template_name = "base/item_detail.html"

    def get_context_data(self, **kwargs):
        context = super(ItemDetailView, self).get_context_data(**kwargs)
        context['price_list'] = ItemPrice.objects.filter(item=context['item'])
        context['price_update'] = ItemPriceFormset()

        return context

def ItemUpdatePrice(request, id):
    if request.method == "POST":
        item = Item.objects.get(pk=1)
        if item not in None:
            update = ItemPriceFormset(request.POST, request.FILES, instance=item)
        else:
            pass

    ....

网址

urlpatterns = patterns ('',
    url(r'^item/update/(?P<id>\d+)$', views.ItemUpdatePrice, name="itemUpdate"),

item_detail.html

//jquery
    $( "#update-form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Update": function() {
                // verify input

                $("#update_price_form").submit();
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });

...
<div id="update-form" title="Update Price">
<p class="validateTips">All form fields are required.</p>
<form id="update_price_form" method="POST">
{% csrf_token %}
{% for update in price_update %}
    <label for="unit-price">Unit Price</label>
    {{ update.unit_price }}
    <label for="cost-price">Cost Price</label>
    {{ update.cost_price }}
    <label for="note">Note</label>
    {{ update.note }}
    {{ price_update.management_form  }}
    {{ update.date }}
{% endfor %}
</form>
</div>
....

1 个答案:

答案 0 :(得分:4)

您的表单没有action属性:<form id="update_price_form" method="POST">。这意味着它将发布到当前活动的URL,该URL将路由到ItemDetailView视图。由于DetailView仅实现了get方法,因此不会出现405 metod错误。

您可以通过添加action这样的属性来解决此问题:<form id="update_price_form" method="POST" action="{% url "itemUpdate" %}">,希望通过您正在使用的jQuery来获取。