查看不会发帖

时间:2011-03-09 15:16:23

标签: django jquery django-views html-post

有人可以告诉我为什么以下视图不会接收POST请求:

# Loads all the latest phone numbers for the models.py file
def client_phones_form_view(request, clientKEY):
    try:
        i_clientKEY = int(clientKEY)
    except ValueError:
        raise Http404()
    phones = []
    # Populates a list with the latest phone numbers for ALL types of phone
    for k, v in PHONE_CHOICES:
        try:
            phones.append(ClientPhone.objects.filter(client=i_clientKEY, phone_type=k).latest('id'))
        except ClientPhone.DoesNotExist:
            pass
    if request.method == 'POST':
        formPhone = ClientPhoneForm(request.POST)
        if formPhone.is_valid() and formPhone.number.clean(formPhone.number):
            c = Client.objects.get(id=i_clientKEY)
            formPhone.CustomSave(c, request.user)
            return render_to_response(...)
        else:
            return render_to_response(...)
    else:
        formPhone = ClientPhoneForm()
        return render_to_response(...)

我知道当我提交表单时它正在重新加载,但它总是重新加载底部render_to_response

修改

javascript看起来像这样:

$( "#newPhoneDialog" ).dialog({
    autoOpen: false,
    height: 200,
    width: 350,
    modal: true,
    buttons: {
        "Add New Phone Number": function() {
            var bValid = true;
            //alert($('#id_number').val()+" - "+$('#id_phone_type').val());

            bValid = bValid && checkNumberLength( phoneNumber, "the phone number", 11, 15 );

            bValid = bValid && checkNumberRegexp( phoneNumber, /^([0-9])|( \t)+$/, "The phone number may only consist of numbers and spaces");

            if ( bValid ) {
                $('.error').hide(); // hide the error div
                $('.success').hide(); // hide the success div
                $('.info').hide(); // hide the information div
                $.ajax({ // create an AJAX call...
                    data: $('#newPhoneForm').serialize(), // get the form data
                    type: 'POST', // GET or POST
                    url: 'client/form/phones/1/', // the file to call
                    success: function(response) { // on success..
                        $('#clientPhonesForm').html(response); // update the main phones DIV
                    }
                });
                $( this ).dialog( "close" );
                //return false; // cancel original event to prevent form submitting
            }
        },
        Cancel: function() {
            // reloads the phone div because re-selecting the first item in the options doesn't work
            $('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}');
            $( this ).dialog( "close" );
        }
    },
    close: function() {
        // reloads the phone div because re-selecting the first item in the options doesn't work
        //$('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}');
    }
});

HTML就像这样:

<div id="newPhoneDialog" title="Create New Phone Number">
        Please enter a valid phone number:
    <div id="container">
        <form action="{% url client.views.client_phones_form_view clientKEY %}" method="POST" id="newPhoneForm">
            <table>
            {% for field in formPhone %}
            <tr><td>{{ field.label_tag }}</td><td>{{ field }}</td></tr>
            {% endfor %}
            </table>
        </form>
    </div>
</div>

修改

如果它说'当我点击按钮时,我如何(jquery ajax)提交表单

2 个答案:

答案 0 :(得分:2)

这是一个防弹条件:if request.method == 'POST'所以我会开始研究为什么你没有生成POST请求,而不是视图。

您能否确保从表单中发帖?

<form method="post">

开发服务器是否注册了POST请求?应该说"GET /...""POST /..."


更新:好的,这是一个ajax请求表单。最近SO上有另一个人有类似的问题,因为AJAX请求触发了原始表单作为GET请求。

我看到你注释掉return false - 这可能是问题吗?实际上没关系,我不认为对话框按钮会干扰。

更重要的是:什么是开发服务器录制?一个POST然后GET?一个GET?

答案 1 :(得分:0)

以下似乎是答案。是否正确我不知道:

# Loads all the latest phone numbers for the models.py file
def client_phones_form_view(request, clientKEY):
    try:
        i_clientKEY = int(clientKEY)
    except ValueError:
        raise Http404()
    phones = []
    # Populates a list with the latest phone numbers for ALL types of phone
    for k, v in PHONE_CHOICES:
        try:
            phones.append(ClientPhone.objects.filter(...).latest('id'))
        except ClientPhone.DoesNotExist:
            pass
    if request.method == 'POST':
        formPhone = ClientPhoneForm(request.POST)
        if formPhone.is_valid():
            try:
                n = formPhone.cleaned_data['number']
            except ValidationError:
                return render_to_response(...)
            c = Client.objects.get(id=i_clientKEY)
            formPhone.CustomNumberSave(c, n, request.user)
            return render_to_response(...)
        else:
            return render_to_response(...)

    else:
        formPhone = ClientPhoneForm()
        return render_to_response(...)