Essex
Essex

Reputation: 6128

Update Form with Django from GET value

I'm creating a function which let to handle forms updating. In my view, user gives an ID, then the script takes this ID and display the corresponding form with the possibility to update data.

My function looks like :

def Identity_Update(request) :

    query_update = request.GET.get('q4')
    if query_update :
        query_update_list = Identity.objects.filter(id__iexact=query_update)    
    else :
        query_update_list = Identity.objects.none() # == []

    template_name = 'edit.html'

    ############

    instance = Identity.objects.get(id = ??? ) # ID have to be egal to query_update
    form = IdentityForm(request.POST or None, instance = instance)

    if form.is_valid():
        form.save()
        return HttpResponseRedirect('accueil')

    context = {
        "query_update" : query_update,
        "query_update_list" : query_update_list,
        "form": form,
        "instance" : instance
        }

    return render(request, template_name, context)

My template looks like :

<!-- ############################################## -->
<!-- Modifier un formulaire dans la Base de Données -->
<!-- ############################################## -->

<h2 align="center"> Modification des fiches individuelles </align> </h2>

{% block content %}

<h4> ID du formulaire à modifier : </h4>

<form method="GET" action="">{% csrf_token %}
    <input type="text"  name="q4" placeholder="Entrer un ID" value="{{ request.GET.q4 }}">
    <input type="submit" value="Valider l'ID">
</form>

<ul>
    Personne concernée : 
    <br></br>
{% for item in query_update_list %}
   <li> {{ item }} </li>
{% endfor %}
</ul>

<br></br>

<h4> Modification du formulaire : </h4>

<form method='POST' action= "" > {% csrf_token %}

{{form.as_ul}}

<input type ="submit" value="Valider" /> 
</form>

{% endblock %}

I get the following problem :

How I could take the value q4 returned by user and place it as id = q4. I don't find a way to make that. In the picture, the form is already filled because I wrote manually an id, but the form should stay empty until I give a value to q4

Thank you very much if you can help me !

enter image description here

PS : Maybe I need to put the form part inside query_update if loop ?

Upvotes: 0

Views: 887

Answers (2)

mymusise
mymusise

Reputation: 399

I think the problem of you is :

when you post this form:

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Update" />
</form> 

the value in your q4 input will not be submit cause they are in different form, so you can'd get the q4(ID?) value in your view. So I suggest you should post then on the same form.

Upvotes: 0

neverwalkaloner
neverwalkaloner

Reputation: 47354

Try to override get_object() method of IdentityUpdate class to select specific object from request's GET parameters.

class IdentityUpdate(UpdateView) :

    model = Identity
    form_class = IdentityForm
    fields = '__all__'
    template_name = 'resume.html'

    def get_object(self):
        query_update = request.GET.get('q4')
        return get_object_or_404(Identity, pk=query_update)

If you prefer to work with function based view try to change Identity_Update() view to this:

def Identity_Update(request) :

    # ID object given by user
    query_update = request.GET.get('q4')
    obj = get_object_or_404(Identity, pk=query_update)

    if query_update : 
        updating_form = IdentityForm(request.POST, instance = obj)
    ....

Upvotes: 1

Related Questions