Django - Editing tables (Update)

Asked

Viewed 1,309 times

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:

inserir a descrição da imagem aqui

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.

1 answer

4


Question 1

The url will be handled by Django middleware and he will then know which id is because you will need to pass this to your field URL Ação table.
Example

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

What will generate as html, for example:

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

About templates tags to the url, when 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

2nd Question

How you are already passing the object reference to the form in the case of the method get:

else:
    form = FormPessoa(instance=oPessoa)

The values should then already appear in the fields if you are creating the form dynamically.
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:"" }}" />

Of these two ways the values will not appear if the record has not been found or if when creating the fields manually you enter wrong property name.

If you do not have a wider answer that removes your doubts, soon I copy your project and check how it is.

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

  • 3

    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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.