Repository: wagtail/wagtail
File: wagtail/images/views/images.py (Python)

Code before:
        # Upon validation, the cleaned data is a Collection instance
        return self.filters and self.filters.form.cleaned_data.get("collection_id")

    def get_add_url(self):
        # Pass the collection filter to prefill the add form's collection field
        return set_query_params(
            super().get_add_url(),
            {"collection_id": self.current_collection and self.current_collection.pk},
        )

    def get_filterset_kwargs(self):
        kwargs = super().get_filterset_kwargs()
        kwargs["is_searching"] = self.is_searching
        return kwargs

    def get_next_url(self):
        next_url = self.index_url
        request_query_string = self.request.META.get("QUERY_STRING")
        if request_query_string:
            next_url += "?" + request_query_string
        return next_url

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        view_mode = self.request.GET.get("view", "grid")

        context.update(
            {
                "next": self.get_next_url(),
                "current_collection": self.current_collection,
                "current_ordering": self.ordering,
                "ORDERING_OPTIONS": self.ORDERING_OPTIONS,
                "view_mode": view_mode,
            }
        )

        return context


class EditView(generic.EditView):
    permission_policy = permission_policy
    pk_url_kwarg = "image_id"
    error_message = gettext_lazy("The image could not be saved due to errors.")
    template_name = "wagtailimages/images/edit.html"
    index_url_name = "wagtailimages:index"
    edit_url_name = "wagtailimages:edit"
    delete_url_name = "wagtailimages:delete"
    url_generator_url_name = "wagtailimages:url_generator"
    header_icon = "image"
    context_object_name = "image"

Review comment (performance):
This is probably worth extracting into a cached property on the view, e.g.

```py
@cached_property
def view_mode(self):
    return self.request.GET.get("view", "grid")
```

So you can use it in other methods/properties as `self.view_mode`, e.g. when you're defining the `columns`.

And if we do want to preserve it in the session, we could do something like

```py
@cached_property
def view_mode(self):
    preferred_view = self.request.GET.get("view")
    if preferred_view:
        request.session["wagtailimages_view_mode"] = preferred_view
    else:
        preferred_view = request.session.get("wagtailimages_view_mode", "grid")
    return preferred_view
```

Code after:
            .prefetch_renditions("max-165x165")
        )
        return images

    @cached_property
    def current_collection(self):
        # Upon validation, the cleaned data is a Collection instance
        return self.filters and self.filters.form.cleaned_data.get("collection_id")

    def get_add_url(self):
        # Pass the collection filter to prefill the add form's collection field
        return set_query_params(
            super().get_add_url(),
            {"collection_id": self.current_collection and self.current_collection.pk},
        )

    def get_filterset_kwargs(self):
        kwargs = super().get_filterset_kwargs()
        kwargs["is_searching"] = self.is_searching
        return kwargs

    def get_next_url(self):
        next_url = self.index_url
        request_query_string = self.request.META.get("QUERY_STRING")
        if request_query_string:
            next_url += "?" + request_query_string
        return next_url

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        context.update(
            {
                "next": self.get_next_url(),
                "current_collection": self.current_collection,
                "current_ordering": self.ordering,
                "ORDERING_OPTIONS": self.ORDERING_OPTIONS,
                "layout": self.layout,
            }
        )

        return context

    @cached_property
    def layout(self):
        return self.request.GET.get("layout", "grid")

    @cached_property
    def columns(self):
        if self.layout == "grid":
            return []