<commit_before>
.. _model_recipes:

Recipes
=======

Overriding the :meth:`~wagtail.models.Page.serve` Method
--------------------------------------------------------------------

Wagtail defaults to serving :class:`~wagtail.models.Page`-derived models by passing a reference to the page object to a Django HTML template matching the model's name, but suppose you wanted to serve something other than HTML? You can override the :meth:`~wagtail.models.Page.serve` method provided by the :class:`~wagtail.models.Page` class and handle the Django request and response more directly.

Consider this example from the Wagtail demo site's ``models.py``, which serves an ``EventPage`` object as an iCal file if the ``format`` variable is set in the request:

.. code-block:: python

    class EventPage(Page):
        ...

        def serve(self, request):
            if "format" in request.GET:
                if request.GET['format'] == 'ical':
                    # Export to ical format
                    response = HttpResponse(
                        export_event(self, 'ical'),
                        content_type='text/calendar',
                    )
                    response['Content-Disposition'] = 'attachment; filename=' + self.slug + '.ics'
                    return response
                else:
                    # Unrecognised format error
                    message = 'Could not export event\n\nUnrecognised format: ' + request.GET['format']
                    return HttpResponse(message, content_type='text/plain')
            else:
                # Display event page as usual
                return super().serve(request)
<commit_msg>Update documentation for the serve method override so that it does not mention a specific demo site. (#8714)<commit_after>
.. _model_recipes:

Recipes
=======

Overriding the :meth:`~wagtail.models.Page.serve` Method
--------------------------------------------------------------------

Wagtail defaults to serving :class:`~wagtail.models.Page`-derived models by passing a reference to the page object to a Django HTML template matching the model's name, but suppose you wanted to serve something other than HTML? You can override the :meth:`~wagtail.models.Page.serve` method provided by the :class:`~wagtail.models.Page` class and handle the Django request and response more directly.

Consider this example of an ``EventPage`` object which is served as an iCal file if the ``format`` variable is set in the request:

.. code-block:: python

    class EventPage(Page):
        ...

        def serve(self, request):
            if "format" in request.GET:
                if request.GET['format'] == 'ical':
                    # Export to ical format
                    response = HttpResponse(
                        export_event(self, 'ical'),
                        content_type='text/calendar',
                    )
                    response['Content-Disposition'] = 'attachment; filename=' + self.slug + '.ics'
                    return response
                else:
                    # Unrecognised format error
                    message = 'Could not export event\n\nUnrecognised format: ' + request.GET['format']
                    return HttpResponse(message, content_type='text/plain')
            else:
                # Display event page as usual
                return super().serve(request)

{'fraction_non_alphanumeric': 0.1290227339828757, 'fraction_numerical': 0.00118098612341305, 'mean_word_length': 1.8907849829351535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938095', 'n_tokens_mistral': 826, 'n_tokens_neox': 772, 'n_words': 291}