Friday, 1 March 2019

Django doesn't update html template after `model.save()`

I have a Django application with DRF. In some moment I do an Ajax call that change a simple field of the model:

# Delete media
def delete(self, request, pk, format=None):
    media = get_object_or_404(Media, pk=pk)
    media.deleted = True
    media.save()
    return Response(status=status.HTTP_200_OK)

After that the Ajax call just make a location.reload(); but the reloaded page still contain the "deleted" model. On the database the deleted field is True, and on my template I make this:



So if I reload the python app it not show the media.deleted objects.

Edited

The Ajax request is:

controller: function ($scope) {
            $scope.execute = function(url){
                console.log($scope.action)

                $.ajax({
                    url: $scope.action,
                    type: 'DELETE',
                    success: function(result){
                        location.reload();
                    },
                    error: function(e){
                        alert("Error deleting")
                        console.log(e)
                    }
                });
            }
        }

When I modify the objects using Django a normal html post form (for example when create an object), the page reloads properly.



from Django doesn't update html template after `model.save()`

No comments:

Post a Comment