Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用django表单编辑模型数据_Python_Django_Django Forms - Fatal编程技术网

Python 如何使用django表单编辑模型数据

Python 如何使用django表单编辑模型数据,python,django,django-forms,Python,Django,Django Forms,我是django的新手,所以很抱歉我的新手问题 我有一个模型,我需要让用户使用django表单或任何其他方式编辑其中的数据。 看看上面的图片,我想显示这个表单,它已经准备好填充数据,并让用户更新它。 最好的方法是什么? 编辑:这是我的views.py代码 def exam_Edit(request,examName,number=0): numner = int(number) number = int(number) questionNo = int(numner)

我是django的新手,所以很抱歉我的新手问题 我有一个模型,我需要让用户使用django表单或任何其他方式编辑其中的数据。

看看上面的图片,我想显示这个表单,它已经准备好填充数据,并让用户更新它。 最好的方法是什么? 编辑:这是我的views.py代码

def exam_Edit(request,examName,number=0):
    numner = int(number)
    number = int(number)
    questionNo = int(numner)
    Myexam = models.Exam.objects.get(name = examName)



    QuestionsAll = models.Question.objects.filter(exam = Myexam)

    myQeustion = Question.objects.filter(exam = Myexam)[nextQuestion]

    answer1 =  models.Asnwers.objects.filter(question=myQeustion)[0]
    answer2 =  models.Asnwers.objects.filter(question=myQeustion)[1]
    answer3 =  models.Asnwers.objects.filter(question=myQeustion)[2]
    answer4 = models.Asnwers.objects.filter(question=myQeustion)[3]


    # HERE IS MY PROBLEM : the line below creates a form with a data but it doesn't save it to the save object     
    form = QuestionsEditForm(initial = {'questionText':myQeustion.__unicode__() , 'firstChoiceText':answer1.__unicode__(),'secondChoiceText':answer2.__unicode__(),'thirdChoiceText':answer3.__unicode__(),'forthChoiceText':answer4.__unicode__()})

    if request.method =='POST':
        #if post 

        if form.is_valid():
            questionText = form.cleaned_data['questionText']
            Myexam = Exam.objects.get(name = examName)
            myQeustion.questionText = form.cleaned_data['questionText']



            answer1.answerText = form.cleaned_data['firstChoiceText']
            answer1.save()

            answer2.answerText = form.cleaned_data['secondChoiceText']
            answer2.save()

            answer3.answerText = form.cleaned_data['thirdChoiceText']
            answer3.save()

            answer4.answerText = form.cleaned_data['forthChoiceText']
            answer4.save()

 variables = RequestContext(request,     {'form':form,'examName':examName,'questionNo':str(nextQuestion)})
 return render_to_response('exam_edit.html',variables)               
请帮助

假设您使用的是ModelForm,请使用instance关键字参数,并传递要更新的模型

因此,如果您有MyModel和MyModelForm,后者必须扩展django.forms.ModelForm,那么您的代码片段可能如下所示:

my_record = MyModel.objects.get(id=XXX)
form = MyModelForm(instance=my_record)
然后,当用户通过POST发回数据时:

form = MyModelForm(request.POST, instance=my_record)

顺便说一句,ModelForm的文档在这里:

的哪一部分比较混乱?你能说得更具体些吗?你能发布你的代码吗?为什么不发布一些你已经有的代码呢?这就像返回一个带有默认文本填充字段的问题模型对象一样简单。只需抬头,但代码的最后两行没有缩进。@S.Lott:Nothing。然而,这里面没有任何东西可以回答这个问题,这不是最好的答案。您刚才描述的逻辑由管理模块实现,具有权限和所有权限。必须有一种方法在前端使用它。@oneloop,即使要在前端实现“编辑数据”功能,也需要将数据发送到后端进行更新。我假设您将创建一个完整的视图,并从那里传递带有请求的工作