मैं नया हूँ django ताकि मैं अपने नौसिखिया सवाल के लिए खेद है
मैं एक मॉडल है कर रहा हूँ और मैं 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)
मदद कृपया
के किस भाग http://docs.djangoproject.com/en/1.3/ref/forms/api/#bound-and-unbound-forms भ्रमित था? क्या आप अधिक विशिष्ट हो सकते हैं? क्या आप अपना कोड पोस्ट कर सकते हैं? –
आप पहले से मौजूद कोड में से कुछ क्यों पोस्ट नहीं करते हैं? यह उन "फ़ील्ड" मॉडल ऑब्जेक्ट को वापस लौटने के समान सरल है जो आप डिफ़ॉल्ट टेक्स्ट के साथ पॉप्युलेट करते हैं। – Nix
बस एक सिर ऊपर है, लेकिन आपके कोड की अंतिम 2 लाइनें इंडेंट नहीं हैं। – john2x