25

i'm new to django so i'm sorry for my newbie question
i have a model and i need to let user edit data inside it using django forms or any other way. enter image description here

look at the image above , i want to show this form ready populated with the data and let user update it.
what is the best way to do this ?

EDIT : here is my views.py code

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)               

please help

4
  • 1
    What part of docs.djangoproject.com/en/1.3/ref/forms/api/… was confusing? Can you be more specific? Can you post your code? Commented May 16, 2011 at 21:08
  • Why don't you post some of the code you already have? This is as simple as returning a "Question" model object with fields that you populate with default text. Commented May 16, 2011 at 21:09
  • Just a heads up, but the last 2 lines of your code aren't indented. Commented May 17, 2011 at 7:37
  • 1
    @S.Lott: Nothing. However, there is nothing in there that answers the question. Commented Mar 1, 2013 at 16:26

1 Answer 1

68

Assuming you are using a ModelForm, use the instance keyword argument, and pass the model you are updating.

So, if you have MyModel and MyModelForm (the latter of which must extend django.forms.ModelForm), then your code snippet might look like:

my_record = MyModel.objects.get(id=XXX)
form = MyModelForm(instance=my_record)

And then, when the user sends back data by POST:

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

Incidentally, the documentation for ModelForm is here: http://docs.djangoproject.com/en/1.8/topics/forms/modelforms/

Sign up to request clarification or add additional context in comments.

2 Comments

This cannot be the best answer. The logic that you just described is implemented by the admin module, with permissions and all. There must be a way to use that on the frontend.
@oneloop, even if you were to implement an edit data feature on the frontend, you need to send data to the backend to update. I would assume you would create an entire view and pass work with the request from there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.