reversing_urls.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. .. _modeladmin_reversing_urls:
  2. =========================
  3. Reversing ModelAdmin URLs
  4. =========================
  5. It's sometimes useful to be able to derive the ``index`` (listing) or
  6. ``create`` URLs for a model along with the ``edit``, ``delete`` or
  7. ``inspect`` URL for a specific object in a model you have registered via
  8. the ``modeladmin`` app.
  9. Wagtail itself does this by instantiating each ``ModelAdmin`` class you have
  10. registered, and using the ``url_helper`` attribute of each instance to
  11. determine what these URLs are.
  12. You can take a similar approach in your own code too, by creating a
  13. ``ModelAdmin`` instance yourself, and using its ``url_helper``
  14. to determine URLs.
  15. See below for some examples:
  16. .. contents::
  17. :local:
  18. :depth: 1
  19. -------------------------------------------------------------------
  20. Getting the ``edit`` or ``delete`` or ``inspect`` URL for an object
  21. -------------------------------------------------------------------
  22. In this example, we will provide a quick way to ``edit`` the Author that is
  23. linked to a blog post from the admin page listing menu. We have defined
  24. an ``AuthorModelAdmin`` class and registered it with Wagtail to allow
  25. ``Author`` objects to be administered via the admin area. The ``BlogPage``
  26. model has an ``author`` field (a ``ForeignKey`` to the ``Author`` model)
  27. to allow a single author to be specified for each post.
  28. .. code-block:: python
  29. # file: wagtail_hooks.py
  30. from wagtail.admin.widgets import PageListingButton
  31. from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
  32. from wagtail.core import hooks
  33. # Author & BlogPage model not shown in this example
  34. from models import Author
  35. # ensure our modeladmin is created
  36. class AuthorModelAdmin(ModelAdmin):
  37. model = Author
  38. menu_order = 200
  39. # Creating an instance of `AuthorModelAdmin`
  40. author_modeladmin = AuthorModelAdmin()
  41. @hooks.register('register_page_listing_buttons')
  42. def add_author_edit_buttons(page, page_perms, is_parent=False, next_url=None):
  43. """
  44. For pages that have an author, add an additional button to the page listing,
  45. linking to the 'edit' page for that author.
  46. """
  47. author_id = getattr(page, 'author_id', None)
  48. if author_id:
  49. # the url helper will return something like: /admin/my-app/author/edit/2/
  50. author_edit_url = author_modeladmin.url_helper.get_action_url('edit', author_id)
  51. yield PageListingButton('Edit Author', author_edit_url, priority=10)
  52. modeladmin_register(AuthorModelAdmin)
  53. As you can see from the example above, when using ``get_action_url()`` to
  54. generate object-specific URLs, the target object's primary key value must be supplied
  55. so that it can be included in the resulting URL (e.g. ``"/admin/my-app/author/edit/2/"``).
  56. The following object-specific action names are supported by ``get_action_url()``:
  57. ``'edit'``
  58. Returns a URL for updating a specific object.
  59. ``'delete'``
  60. Returns a URL for deleting a specific object.
  61. ``'inspect'``
  62. Returns a URL for viewing details of a specific object.
  63. **NOTE:** This will only work if ``inspect_view_enabled`` is set to
  64. ``True`` on your ``ModelAdmin`` class.
  65. .. note::
  66. If you are using string values as primary keys for you model, you may need to handle
  67. cases where the key contains characters that are not URL safe. Only alphanumerics
  68. (``[0-9a-zA-Z]``), or the following special characters are safe:
  69. ``$``, ``-``, ``_``, ``.``, ``+``, ``!``, ``*``, ``'``, ``(``, ``)``.
  70. ``django.contrib.admin.utils.quote()`` can be used to safely encode these primary
  71. key values before passing them to ``get_action_url()``. Failure to do this may result
  72. in Wagtail not being able to recognise the primary key when the URL is visited,
  73. resulting in 404 errors.
  74. ---------------------------------------------------
  75. Getting the ``index`` or ``create`` URL for a model
  76. ---------------------------------------------------
  77. There are URLs available for the model listing view (action is ``'index'``) and
  78. the create model view (action is ``'create'``). Each of these has an equivalent
  79. shortcut available; ``url_helper.index_url`` and ``url_helper.create_url``.
  80. For example:
  81. .. code-block:: python
  82. from .wagtail_hooks import AuthorModelAdmin
  83. url_helper = AuthorModelAdmin().url_helper
  84. index_url = url_helper.get_action_url('index')
  85. # OR we can use the 'index_url' shortcut
  86. also_index_url = url_helper.index_url # note: do not call this property as a function
  87. # both will output /admin/my-app/author
  88. create_url = url_helper.get_action_url('create')
  89. # OR we can use the 'create_url' shortcut
  90. also_create_url = url_helper.create_url # note: do not call this property as a function
  91. # both will output /admin/my-app/author/create
  92. .. note::
  93. If you have registered a page type with ``modeladmin`` (e.g. ``BlogPage``), and pages
  94. of that type can be added to more than one place in the page tree, when a user visits
  95. the `create` URL, they'll be automatically redirected to another view to choose a
  96. parent for the new page. So, this isn't something you need to check or cater for in
  97. your own code.
  98. To customise ``url_helper`` behaviour, see :ref:`modeladmin_url_helper_class`.