如何使用django和ajax创建函数更新

如何使用django和ajax创建函数更新,django,ajax,forms,sql-update,Django,Ajax,Forms,Sql Update,我有一个django项目,其中包括一个表单,用户可以在其中插入数据,并使用django函数和ajax调用将其保存到数据库中 不使用django中的模型形式 现在我想允许用户更新他选择的表单,一旦用户选择表单,字段必须显示现有数据 到目前为止,这是一个创建过程 我知道更新过程需要对象的id才能更新所选记录 错误: “可疑”对象不是iterable请求方法:获取请求 URL:Django版本:2.1.3 异常类型:TypeError异常值:“可疑”对象不是 iterable异常位置:C:\Users

我有一个django项目,其中包括一个表单,用户可以在其中插入数据,并使用django函数和ajax调用将其保存到数据库中

不使用django中的模型形式

现在我想允许用户更新他选择的表单,一旦用户选择表单,字段必须显示现有数据

到目前为止,这是一个创建过程

我知道更新过程需要对象的id才能更新所选记录

错误:

“可疑”对象不是iterable请求方法:获取请求 URL:Django版本:2.1.3 异常类型:TypeError异常值:“可疑”对象不是 iterable异常位置:C:\Users\LT GM\AppData\Local\Programs\Python\Python37\lib\site packages\django\template\defaulttags.py 在render中,第165行Python可执行文件:C:\Users\LT GM\AppData\Local\Programs\Python\Python37\Python.exe

url.py
我将非常感谢任何帮助

我将为您提供一个简单的示例,您可以扩展到您的案例中

在模板中,用户有一个更新其配置文件的链接:

<a href="{% url 'Profile-Update' %}"> Update Profile </a>
in views.py


def ProfileUpdate(request):
  current_user = request.user
  if request.method == 'POST':
    get_username = request.POST.get('username', '').strip()
    suspect.objects.filter(pk=current_user.pk).update(username=get_username)
    return HttpResponse('Profile Updated')
  else:
    return render(request, 'prorile_update_form.html', {'current_user': current_user})
在prorile_update_form.html中:


<form class="update_profile">
{{ csrf_token }}
  <input type="text" name="username" value="{{ current_user.username }}">   
  <button class="submit_update">Save changes</button>
</form>
<!-- So the current user username will be displayed on the input as a value -->

<script type="text/javascript">
$('.submit_update').on('click', function(){
              $.ajax({
             url: '/update_profile/',
             method : 'POST',
             data: $('.update_profile').serialize(),
             beforeSend: function() {
              // things to do before submit
             },
             success: function(response) {

              alert(response)
              }
              });
  return false;

});
</script>


您可以使用pk获取所需的模型,然后根据您将提供的表单进行所有所需的更新。

我将编辑我的问题并添加对函数和模板所做的更新。您能看一下并告诉我错误是什么吗?因为系统显示一个错误。如果我的回答对你的第一个问题是正确的,你能接受吗?我如何检查它是否对我有效?我确实更新了问题并添加了功能,如果你能检查它,告诉我它是否写错了,错误在哪里。我会接受你的回答,感谢你的时间和帮助,但希望你能回答更新后的问题。请注意,进入更新页面是通过一个名为“更新详细信息”的按钮然后,用户可以根据所选id更新表单上现有的数据
<a href="{% url 'Profile-Update' %}"> Update Profile </a>
path('update_profile/', views.ProfileUpdate, name='Profile-Update')

def ProfileUpdate(request):
  current_user = request.user
  if request.method == 'POST':
    get_username = request.POST.get('username', '').strip()
    suspect.objects.filter(pk=current_user.pk).update(username=get_username)
    return HttpResponse('Profile Updated')
  else:
    return render(request, 'prorile_update_form.html', {'current_user': current_user})

<form class="update_profile">
{{ csrf_token }}
  <input type="text" name="username" value="{{ current_user.username }}">   
  <button class="submit_update">Save changes</button>
</form>
<!-- So the current user username will be displayed on the input as a value -->

<script type="text/javascript">
$('.submit_update').on('click', function(){
              $.ajax({
             url: '/update_profile/',
             method : 'POST',
             data: $('.update_profile').serialize(),
             beforeSend: function() {
              // things to do before submit
             },
             success: function(response) {

              alert(response)
              }
              });
  return false;

});
</script>

path('update_profile/<int:pk>', views.ProfileUpdate, name='Profile-Update')
def ProfileUpdate(request, pk):