4
In my application I have a function to search 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 details of the search are returned in table this way:
where the Code field is the pk (Primary Key) of the returned objects. In the table in the Action field I have the edit link that directs to the following view:
@login_required
def upPessoa(request, id):
oPessoa = Pessoa.objects.get(pk=id)
if request.method == 'POST':
form = FormPessoa(request.POST, instance=oPessoa)
if form.is_valid():
form.save()
return HttpResponseRedirect('')
else:
form = FormPessoa(instance=oPessoa)
return render(request,'template.html',
{
'form':form,
'codigo':id,
}
)
my url is defined like this:
url(r'^editar/usuario/(?P<id>\d+)/$', 'library.views.upPessoa', name='nUpPessoa'),
I have 2 doubts:
1- How will the url know that the object I want to edit is x or y ?! More specifically how she will know that the id to be received in the expression (?P<id>\d+)
and transmitted to the view is 2 or 9 ?!
2 - When being redirected to the edition page I would like the fields to be already filled with the registered information, I verified that I have to use a initial
and pass it as context in the view only that I didn’t quite understand how this initial
works.
If you need me, my entire project is on Github
Note: The views and urls are not as mentioned above, as I haven’t committed the modifications yet.
Note 2: I am still a beginner in Django I started studying a little while ago.
At first I was using the template tag "{% url 'library.views.upPessoa' %}" in href of the Edit link, to mount the path from the desired view. Then I modified it to: href="edit/user/{{ pessoa.pk }}", and I also had to modify my Regex from the url to: r' .+ /edit/user/(?P<id> d+)/$'. One more question: Would you have some way to pass this id to view otherwise to change the template tag ?! I appreciate your help.
– water