Django - Editing Tables (Update)

4

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,IhaveverifiedthatIhavetouseainitialandpassitascontextintheviewsothatIdonotquiteunderstandhowthisinitialworks.

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.

    
asked by anonymous 10.06.2014 / 21:12

1 answer

4

1st Question

The url will be handled by Django's middlewares and it will then know what the Id is because you will need to pass this to the URL of your%> field of the table. Example

<td>
    <a href="/pesquisa/usuario/{{ codigo }}">Editar</a>
<td>

What you will generate as html, for example:

<td>
    <a href="/pesquisa/usuario/2">Editar</a>
<td>

templates tags for the url, when it is necessary, you need to enter additional parameters. Examples:

{% url 'nUpPessoa' v1 v2 %}

{% url 'nUpPessoa' arg1=v1 arg2=v2 %}

{% url 'nUpPessoa' client.id %}

source: docs.djangoproject / templates

Question 2

As you are already passing the object reference to the form in the case of the Ação method:

else:
    form = FormPessoa(instance=oPessoa)

The values should then already appear in the fields if you are dynamically creating the form. Example of dynamic creation:

{% for field in form %}
    {{ field }}
{% endfor %}

But if you are creating the fields / elements manually then you can pass the value through the form. Example:

<input type="text" id="login" name="login" 
    value="{{ form.login.value|default_if_none:"" }}" />

In these two ways, the values will not appear if the record was not found or if you manually enter the property name when you create the fields.

If you do not get a bigger answer and you remove your doubts, I'll soon copy your project and check it out as it is.

More about working with forms in Django: Django Brazil - Forms

    
10.06.2014 / 21:35