8000 Explain how to route requests from an apphook back to the CMS · django-cms/django-cms@85a7c25 · GitHub
[go: up one dir, main page]

Skip to content

Commit 85a7c25

Browse files
committed
Explain how to route requests from an apphook back to the CMS
1 parent 92a1086 commit 85a7c25

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

docs/how_to/11-apphooks.rst

Lines changed: 37 additions & 8 deletions
184
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ example:
3434
name = "My Apphook"
3535
3636
def get_urls(self, page=None, language=None, **kwargs):
37-
return ["myapp.urls"] # replace this with the path to your application's URLs module
37+
return ["myapp.urls"] # replace this with the path to your application's URLs module
3838
3939
Apphooks for namespaced applications
4040
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -136,8 +136,8 @@ So, given an application with the ``urls.py`` for the views ``index_view`` and
136136
.. code-block::
137137
138138
urlpatterns = [
139-
path('archive/', archive_view),
140-
path('', index_view),
139+
path("archive/", archive_view),
140+
path("", index_view),
141141
]
142142
143143
attached to a page whose URL path is ``/hello/world/``, the views will be exposed as
@@ -149,13 +149,42 @@ follows:
149149
Sub-pages of an apphooked page
150150
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151151

152-
.. important::
152+
Usually you should not add child pages to a page with an apphook. This is because the
153+
apphook "swallows" all URLs below that page, handing them over to the attached application.
153154

154-
Don't add child pages to a page with an apphook.
155+
In the rare occasion that you nevertheless want to add child pages below an apphooked
156+
page, then you must add a special URL pattern to route requests back into the CMS.
157+
158+
For example, if you have an apphooked page at ``/hello/`` and you want to add a CMS page,
159+
and optionally its children below that page using the slug ``world``, then rewrite the
160+
URL patterns from above as:
161+
162+
.. code-block:: python
163+
164+
from django.urls import path, re_path
165+
from cms.views import details
166+
167+
def reroute_cms_page(request, path, page=None):
168+
language = get_language_from_request(request, check_path=True)
169+
return details(request, f'{page.get_path(language)}/{path}')
170+
171+
class MyApphook(CMSApp):
172+
# ...
173+
def get_urls(self, page=None, language=None, **kwargs):
174+
return [
175+
path("archive/", archive_view),
176+
re_path(r"^(?P<path>world/.*)$", reroute_cms_page, {"page": page}),
177+
path("", index_view),
178+
]
179+
180+
Here we created a short function-based view named ``reroute_cms_page``. It handles
181+
the requests which otherwise would be swallowed by the apphook.
182+
183+
A requests starting with the URL ``/hello/`` then is handled by ``index_view``,
+
``/hello/archive/`` is handled by ``archive_view``, and ``/hello/world/``,
185+
``/hello/world/foo``, etc. are handled by our special view ``reroute_cms_page``,
186+
routing the request back to the ``detail()`` view of Django-CMS.
155187

156-
The apphook "swallows" all URLs below that of the page, handing them over to the
157-
attached application. If you have any child pages of the apphooked page, django CMS
158-
will not be able to serve them reliably.
159188

160189
Managing apphooks
161190
-----------------

0 commit comments

Comments
 (0)
0