2011-05-16 17 views
17

मैं नया हूँ django ताकि मैं अपने नौसिखिया सवाल के लिए खेद है
मैं एक मॉडल है कर रहा हूँ और मैं Django रूपों या किसी अन्य तरीके से उपयोग कर इसे अंदर डेटा उपयोगकर्ता संपादित होने देना होता है मॉडल डेटा को संपादित करने के लिए कैसे । enter image description hereDjango रूपों का उपयोग कर

उपरोक्त छवि को देखें, मैं इस फ़ॉर्म को डेटा के साथ तैयार करने के लिए तैयार करना चाहता हूं और उपयोगकर्ता को इसे अपडेट करने देना चाहता हूं।
ऐसा करने का सबसे अच्छा तरीका क्या है?

संपादित करें: यहाँ मेरी 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)    

मदद कृपया

+1

के किस भाग http://docs.djangoproject.com/en/1.3/ref/forms/api/#bound-and-unbound-forms भ्रमित था? क्या आप अधिक विशिष्ट हो सकते हैं? क्या आप अपना कोड पोस्ट कर सकते हैं? –

+0

आप पहले से मौजूद कोड में से कुछ क्यों पोस्ट नहीं करते हैं? यह उन "फ़ील्ड" मॉडल ऑब्जेक्ट को वापस लौटने के समान सरल है जो आप डिफ़ॉल्ट टेक्स्ट के साथ पॉप्युलेट करते हैं। – Nix

+0

बस एक सिर ऊपर है, लेकिन आपके कोड की अंतिम 2 लाइनें इंडेंट नहीं हैं। – john2x

उत्तर

47

मान लें कि आप एक ModelForm उपयोग कर रहे हैं, instance कीवर्ड तर्क का उपयोग, और मॉडल आप अद्यतन कर रहे हैं पार जाते हैं।

तो, आप MyModel और MyModelForm (जिनमें से बाद django.forms.ModelForm का विस्तार करना होगा), तो अपने कोड का टुकड़ा लग सकता है, तो जैसे:

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

और फिर, उपयोगकर्ता पोस्ट द्वारा डेटा वापस भेजता है जब:

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

संयोग से, ModelForm के लिए प्रलेखन यहाँ है: http://docs.djangoproject.com/en/1.8/topics/forms/modelforms/

+0

यह सबसे अच्छा जवाब नहीं हो सकता है। आपके द्वारा वर्णित तर्क को व्यवस्थापक मॉड्यूल द्वारा अनुमतियों और सभी के साथ कार्यान्वित किया गया है। फ्रंटेंड पर इसका उपयोग करने का एक तरीका होना चाहिए। – oneloop

संबंधित मुद्दे