【问题标题】:Migrating a django form into Ajax gives ValueError将 django 表单迁移到 Ajax 会产生 ValueError
【发布时间】:2010-12-19 13:56:08
【问题描述】:

我有一个 django 应用程序,它有几种形式,我正在尝试迁移到 ajax。我这样做是因为我想在不刷新屏幕的情况下更新我的数据。我使用了 dajax 库并开始将我的一些 views.py 代码移动到 ajax.py 中。

在views.py 中运行良好的代码中出现“ValueError: too many values to unpack”。我不确定为什么会发生这种情况或如何解决它。建议?

ajax.py

def send_student_form(request, form):
  dajax = Dajax()
  #error is here
  student = Student.objects.get( form.get('student_id'))
  #student = Student()  # no ValueError, but it doesn't find my student.
  if student:
    print "Student Found!"
    sForm = StudentProfileForm(request.POST, instance=student) 
    print student
  else:
    print "Student Not Found"
    sForm = StudentProfileForm(request.POST)
    #TODO: new Student

【问题讨论】:

标签: ajax django


【解决方案1】:

您需要定义要查询的字段,例如:

# Query by primary key
student = Student.objects.get(pk = form.get('student_id'))

也允许使用非关键字语法,但很难看。非关键字参数是 Django 查询构建过程中的一种特殊情况,如果您想了解更多信息,请参阅 Q 类的构造函数。

student = Student.objects.get(("pk", form.get('student_id')))

【讨论】:

    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 2022-12-01
    • 2013-04-17
    • 2021-08-30
    • 2011-10-22
    相关资源
    最近更新 更多