In my application I have a function fetch a person by name and edit data of a registered person ...
When I search and there is more than one person registered with that name (or even when there is only one person), the search details are returned in a table like this:
wheretheCodefieldisthepk(PrimaryKey)ofthereturnedobjects.InthetableintheActionfieldIhavethelinkforeditingthatdirectstothefollowingview:
@login_requireddefupPessoa(request,id):oPessoa=Pessoa.objects.get(pk=id)ifrequest.method=='POST':form=FormPessoa(request.POST,instance=oPessoa)ifform.is_valid():form.save()returnHttpResponseRedirect('')else:form=FormPessoa(instance=oPessoa)returnrender(request,'template.html',{'form':form,'codigo':id,})
Myurlisdefinedasfollows:
url(r'^editar/usuario/(?P<id>\d+)/$','library.views.upPessoa',name='nUpPessoa'),
Ihave2questions:
1-HowdoestheurlknowthattheobjectIwanttoeditisthexorycode?Morespecificallyhowwillitknowthattheidtobereceivedintheexpression(?P<id>\d+)
andpassedtotheviewiseither2or9?!
2-WhenbeingredirectedtotheeditingpageIwouldlikethefieldstoalreadybefilledwiththeregisteredinformation,IhaveverifiedthatIhavetouseainitial
andpassitascontextintheviewsothatIdonotquiteunderstandhowthisinitial
works.
Ifyouneedit,mycompleteprojectison GitHub
Note: The views and urls are still not as mentioned above, as I have not yet committed the modifications.
Note 2: I'm still a beginner in Django, I started studying shortly.