[Fixed]-Create, get and edit user information in same form and template

1πŸ‘

βœ…

At first glance, it seems that the informacionFacturacion table is not being populated. Have you checked that the instance.save() is reached? (in other words, that the form is valid)

Second, in the template you want to use the informacionFacturacion object as the form elements, and you are handling them separately. Do:

if request.POST:
    form = informacionFacturacionForm(request.POST)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
    else:
        # handle here the form error's, maybe report it in the template
else:
    query = informacionFacturacion.objects.filter(usuario=request.user)
    form = informacionFacturacionForm(instance=query[0])

and render the form parameter insead of infoFacturacion:

{{ form.as_p }}

finally, make sure that your template form id’s matches the form element names, otherwise the form won’t be filled.

UPDATE

Based on your edit, now the error is in this line:

form = informacionFacturacionForm(request.POST, instance=query_id)

query_id is an int, and it is expecting a model. Change the following line:

query_id = informacionFacturacion.objects.get(usuario=request.user).id

to

query = informacionFacturacion.objects.get(usuario=request.user)

and the faulty line to:

form = informacionFacturacionForm(request.POST, instance=query)

that should work for now, although code can be simplified a lot.

EDIT 2

Here is what I assume you want:

@login_required
def datosPersonales(request):
    query = informacionFacturacion.objects.filter(usuario=request.user)

    if request.method == "POST":  # This will handle the template form's POST
        form = informacionFacturacionForm(request.POST)
        if form.is_valid():
            asd = form.save(commit=False)
            asd.save()
            # Here you may want to redirect to somewhere else
    # Im not sure here, I guess that you want to handle the GET method if 
    # there is no form in the request. Post your template form to see what
    # is happening.
    else:
        form = informacionFacturacionForm(instance=query)
        # you dont need to save it, it is already in DB

    context = {
        "titulo": "Datos personales | Co.",
        "body_class": "class= sidebar_main_open sidebar_main_swipe",
        # I think here is your main issue, you are handling a form object
        # AND a infoFacturacion object. You need to use just the
        # form object in the template and render it accordingly.
        "form": form,   
        "infoFacturacion": query, 
    }
    template = "micuenta/datosPersonales.html"
    return render(request, template, context)
πŸ‘€alfonso.kim

0πŸ‘

Well, I was with the same problem on my sytem, so I made this solution, maybe it works to you! =D

I’m changing the value of the submit button and using the same form:

<button type="submit" id="submitButton" name="button" value="">Save</button>

If is a new task, I change the value of the button with JQuery:

$('#submitButton').val('new');

And if is an edition, I change the value again:

$('#submitButton').val('edit');

On my views.py, I check if is an edit or a new save by the value of the button:

def index(request):
tasks = Task.object.filter()
context = {
    'tasks': tasks
}
if request.method == 'POST':
    form = NewTask(request.POST or None)
    if request.POST['button'] == 'new':
        if form.is_valid():
            context['is_valid'] = True
            form.save()
            form = NewTask()
        else:
            context['is_valid'] = False

    if request.POST['button'] == 'edit':
        instance = Task.object.filter(pk=request.POST['id']).first()
        form = NewTask(request.POST, instance=instance)
        if form.is_valid():
            context['is_valid'] = True
            form.save()
        else:
            context['is_valid'] = False
else:
    form = NewTask()

context['form'] = form
return render(request, 'index.html', context)
πŸ‘€Joao Lucas

Leave a comment