@@ -34,7 +34,7 @@ example:
34
34
name = "My Apphook"
35
35
36
36
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
38
38
39
39
Apphooks for namespaced applications
40
40
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -136,8 +136,8 @@ So, given an application with the ``urls.py`` for the views ``index_view`` and
136
136
.. code-block ::
137
137
138
138
urlpatterns = [
139
- path(' archive/' , archive_view),
140
- path('' , index_view),
139
+ path(" archive/" , archive_view),
140
+ path("" , index_view),
141
141
]
142
142
143
143
attached to a page whose URL path is ``/hello/world/ ``, the views will be exposed as
@@ -149,13 +149,42 @@ follows:
149
149
Sub-pages of an apphooked page
150
150
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151
151
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.
153
154
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 ``,
184
+ ``/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.
155
187
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.
159
188
160
189
Managing apphooks
161
190
-----------------
0 commit comments