submarcos / django-vectortiles

Mapbox VectorTiles for django, with PostGIS or Python
https://django-vectortiles.readthedocs.io
MIT License
38 stars 11 forks source link

Django Rest Framework Compatability - Not Accepted 406 #26

Closed StefanBrand closed 1 year ago

StefanBrand commented 2 years ago

I was trying to use a DRF view with django-vectortiles, as mentioned in the README. When I do a tileserver view like this:

class TileServerView(MVTView, views.APIView):
    def get(self, request: Request, *args, **kwargs) -> HttpResponse:
        return BaseVectorTileView.get(
            self,
            request=request,
            z=kwargs.get("z"),
            x=kwargs.get("x"),
            y=kwargs.get("y"),
        )

I get <Response status_code=406, "application/json"> ("Could not satisfy the request Accept header"). This is probably due to DRF's content negotiation.

As a workaround I provide a custom renderer:

class MVTRenderer(renderers.BaseRenderer):
    media_type = "application/vnd.mapbox-vector-tile"
    format = "pbf"

    def render(self, data, accepted_media_type=None, renderer_context=None):
        return data

class TileServerView(MVTView, views.APIView):
    renderer_classes = [MVTRenderer]
    ...

@submarcos If helpful, I can add some hint to the README. I'm even wondering if MVTView shouldn't add renderer_classes by default...

submarcos commented 2 years ago

Hi, thank you @StefanBrand . Yes could propose that in README, even add a MVTRenderer mixin if you want

StefanBrand commented 2 years ago

Hi, @submarcos . I went for a simple addition to the README because I did not want to introduce a dependency on DRF.

I'm happy to make any changes to the PR if you have suggestions. :)