Django表中的数据更新

Django data updation in the table

本文关键字:数据 更新 Django      更新时间:2023-09-26

我正在使用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.
        }

您应该使用正在使用的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;
        });

您可以遵循如何使用AJAX发布django表单;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()。请根据您的要求进行更改。