【问题标题】:Django data updation in the table表中的Django数据更新
【发布时间】:2015-01-07 15:46:49
【问题描述】:

我正在使用 Django 将一些数据输入到我的数据库中。输入数据后,我想对其进行编辑。现在,我正在尝试的是,用户不应该去任何其他页面来更改数据。所以我实现了一个在前端编辑文本的javascript方法。

如何在数据库中反映用户所做的更改?

相关代码如下:

<html>
{% csrf_token  %}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

        <table id="table">
                <tr>
                        <th>Name</th>
                        <th>Phone Number</th>
                </tr>
                {% for record in queryset  %}
                <tr>
                        <td onClick="clickable(this)"> {{record.first}} </td>
                        <td onClick="clickable(this)"> {{record.second}}</td>
                </tr>
                {%endfor%}
        </table>

<script>
function clickable(ele)
{
        var value = prompt("Enter the details");
        if(value)
        {
                ele.id='edited'
                ele.innerHTML = value;
                //I want to send the request to my django view to edit the database here
                //The data has been updated.

        }

【问题讨论】:

    标签: javascript jquery html django


    【解决方案1】:

    您应该使用您正在使用的 jQuery 向您的服务器发送一个 Ajax 请求。使用 Ajax 请求请求,您应该发送更新的数据。
    简单的 Ajax 请求即可。

    $('#click_place').click(function() { // when click is placed
                $.ajax({ // create an AJAX call...
                    data: $(this).serialize(), // get the content here Ex. {'name': 'test', 'phone': '123'}
                    type: $(this).attr('method'), // GET or POST
                    url: $(this).attr('action'), // request url
                    success: function(response) { // on success..
                        // display your message
                    }
                });
                return false;
            });
    

    你可以关注How to POST a django form with AJAX & jQuery
    http://coreymaynard.com/blog/performing-ajax-post-requests-in-django/

    编辑: 您可以在任何情况下简单地调用以下函数。

    function  myajaxhit() {
                    $.ajax({ // create an AJAX call...
                        data: $(this).serialize(), // get the content here Ex. {'name': 'test', 'phone': '123'}
                        type: $(this).attr('method'), // GET or POST
                        url: $(this).attr('action'), // request url
                        success: function(response) { // on success..
                            // display your message
                        }
                    });
    }
    

    只需在任何地方调用 myajaxhit() 即可。请根据您的要求更改它。

    【讨论】:

    • 感谢这个!!我想知道是否可以在不需要单击事件的情况下定义该函数,以便可以从另一个函数调用此函数。在这种情况下,我会将点击事件链接到该函数,此时不需要点击。
    • 完成!您能否编辑答案以告诉我如何在不等待点击的情况下进行操作?
    猜你喜欢
    • 2017-12-02
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2023-03-25
    • 2019-12-16
    相关资源
    最近更新 更多