Modify only one field of the model

I’m not talking about the server side here - my previous reply was talking about what’s necessary in the JavaScript running in the browser.

I think we’ll start the conversation again because I’ve missed a lot.
Define view that passes pk of the record whose field you want to modify.

def AprobarReclamacion(pk):
    
    reclamacion = Rec.objects.filter(id=pk).update(estado_cod=5)
    return redirect('inicio_reclamaciones')

is it correct this way?

Yes. That appears ok, except whether or not that’s “right” depends upon how you’re using this view in the browser.

Sorry but I don’t understand this last sentence “upon how you’re using this view in the browser”

The view is designed to be used by a browser making a request and accepting a response. You can’t create a view without knowing how the browser is going to be using it. (Or, you can create the view anyway you want, but then you need to adjust the code in the browser to make it work with your view.)

Regardless of which way you look at it, it’s a mistake to consider either part (the view or the browser) without being aware of what the other side is doing.

The truth is that I don’t know what you mean, the other views that I have in views.py I did the code in the django project, I didn’t have to do anything else.
Can you give me a code example of what you mean by “adjusting the code in the browser”

do you mean the html or js?

Hello,

I have managed to modify the field with the following,

def AprobarReclamacion(request,pk):
        Rec.objects.filter(id=pk).update(estado_cod=5)
        return redirect('inicio_reclamaciones')
path('reclamaciones/aprobar_reclamacion/<int:pk>/', AprobarReclamacion, name='aprobar_reclamacion'),

The problem I have now is that when I click on the approve button, I have made that there will be a modal and then what happens is that it opens and “closes” the modal but not correctly. If you look at the screenshot on the web, the dark part looks as if the modal was still open. What you would need is to be able to just launch the action with the button without having to open the modal.
The part to do that is here in the onclick but I don’t know how to tell it to simply pass the pk to the url and without having to go through the modal.
response[i][‘pk’]

I’ve tried something like this but without success.

part of the js code of the DataTable

                fila += '<button type = "button" class = "btn btn-primary btn-sm tableButton" ';
                fila += 'onclick = "\'/reclamaciones/aprobar_reclamacion/' + response[i]['pk']+'/\';"> APROBAR </button>';

These two lines are where I must define the button and pass it the url + pk
What would this be in an html page

<a name="" id="" class="btn btn-success" href="{% url 'aprobar_reclamacion' object.pk %}" role="button">Editar</a>

Part of what I was trying to explain earlier is that you need to look at this entire process from start to finish.

You can’t just look at the view. You can’t just look at the JavaScript. You can’t just look at the html. You need to look at all of this together.

Yes, they are separate files. Yes, they execute on different computers. The view executes in the server, the JavaScript runs in the browser. But they must work together to give you the results you’re looking for.

For example, your view has:

This implies that your view is designed to be a browser endpoint and not to be used by an AJAX call. (If it were designed to be used as an AJAX endpoint, it would be returning a JsonResponse and not an HttpRedirect).

As a result of that, your onclick attribute should look something like onclick="window.location.href=\"/reclamaciones/aprobar_reclamacion/\""+pk+"/"

It is that I think that it does not reload the page completely, that is, the browser does not reload the js and then it is shown as the previous image.
I have tried the code

onclick="window.location.href=\"/reclamaciones/aprobar_reclamacion/\""+pk+"/"

I get error: Uncaught SyntaxError: Unexpected end of input

That was just an example - you need to adjust that for your specific situation. It’s only to show that you need to use the window.location.href attribute in JavaScript to have that button request a new page.

I already have it:

onclick = "window.location.href=\'/reclamaciones/aprobar_reclamacion/' + response[i]['pk']+'/\'"
1 Like

Thank you very much for your patience, I appreciate your help.

Greetings,