Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

how_to_change_a_url.rst 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ==============================================
  2. How to add views or change URLs or permissions
  3. ==============================================
  4. Oscar has many views and associated URLs. Often you want to customise these
  5. URLs for your domain, or add additional views to an app.
  6. This how-to describes how to do just that.
  7. It builds upon the steps described in :doc:`/topics/customisation`. Please
  8. read it first and ensure that you've:
  9. * Created a Python module with the the same label
  10. * Added it as Django app to ``INSTALLED_APPS``
  11. * Added a ``models.py`` and ``admin.py``
  12. The app config class
  13. --------------------
  14. Each Oscar app comes with an app config class which inherits from
  15. :class:`oscar.core.application.OscarConfig` or
  16. :class:`oscar.core.application.OscarDashboardConfig`. They're mainly used to
  17. gather URLs (with the correct permissions) for each Oscar app. This structure
  18. makes Oscar apps more modular as each app is responsible for its own URLs. And
  19. as it is a class, it can be overridden like any other Oscar class; hence making
  20. it straightforward to change URLs or add new views.
  21. Each app config instance exposes a ``urls`` property, which is used to access
  22. the list of URLs of an app, together with their application and instance
  23. namespace.
  24. The app config tree
  25. -------------------
  26. Oscar's app config instances are organised in a tree structure. The root app
  27. config class illustrates this nicely::
  28. # oscar/config.py
  29. from django.apps import apps
  30. from oscar.core.application import OscarConfig
  31. class Shop(OscarConfig):
  32. name = 'oscar'
  33. def ready(self):
  34. self.catalogue_app = apps.get_app_config('catalogue')
  35. self.basket_app = apps.get_app_config('basket')
  36. # ...
  37. def get_urls(self):
  38. urls = [
  39. url(r'^catalogue/', self.catalogue_app.urls),
  40. url(r'^basket/', self.basket_app.urls),
  41. # ...
  42. ]
  43. The root app config pulls in the URLs from its children. That means to add
  44. all Oscar URLs to your Django project, you only need to include the list of URLs
  45. (the first element of the ``urls`` property's value) from the root app config::
  46. # urls.py
  47. from django.apps import apps
  48. urlpatterns = [
  49. # Your other URLs
  50. url(r'^', include(apps.get_app_config('oscar').urls[0])),
  51. ]
  52. Changing sub apps
  53. -----------------
  54. :py:class:`~django.apps.config.AppConfig` of sub apps such as the ``catalogue`` app are dynamically
  55. obtained by looking them up in the Django app registry::
  56. # oscar/config.py
  57. from django.apps import apps
  58. from oscar.core.application import OscarConfig
  59. class Shop(OscarConfig):
  60. name = 'oscar'
  61. def ready(self):
  62. self.catalogue_app = apps.get_app_config('catalogue')
  63. self.customer_app = apps.get_app_config('customer')
  64. # ...
  65. That means you just need to create another app config class. It will usually
  66. inherit from Oscar's version. Say you'd want to add another view to the
  67. ``offer`` app. You only need to create a class called ``OfferConfig``
  68. (and usually inherit from Oscar's version) and add your view and its URL
  69. configuration::
  70. # yourproject/offer/apps.py
  71. from oscar.apps.offer.apps import OfferConfig as CoreOfferConfig
  72. from .views import MyExtraView
  73. class OfferConfig(CoreOfferConfig):
  74. def ready(self):
  75. super().ready()
  76. self.extra_view = MyExtraView
  77. def get_urls(self):
  78. urls = super().get_urls()
  79. urls += [
  80. url(r'extra/$', self.extra_view.as_view(), name='extra'),
  81. ]
  82. return self.post_process_urls(urls)
  83. Changing the root app
  84. ---------------------
  85. If you want to e.g. change the URL for the ``catalogue`` app from ``/catalogue``
  86. to ``/catalog``, you need to use a custom root app config class, instead of
  87. Oscar's default class. Hence, create a subclass of Oscar's main ``OscarConfig``
  88. class and override the ``get_urls`` method::
  89. # myproject/apps.py
  90. from oscar import config
  91. class MyShop(config.Shop):
  92. # Override get_urls method
  93. def get_urls(self):
  94. urlpatterns = [
  95. url(r'^catalog/', self.catalogue_app.urls),
  96. # all the remaining URLs, removed for simplicity
  97. # ...
  98. ]
  99. return urlpatterns
  100. # myproject/__init__.py
  101. default_app_config = 'myproject.apps.MyShop'
  102. Then change ``urls.py`` to use your new :py:class:`~django.apps.config.AppConfig`
  103. instead of Oscar's default::
  104. # urls.py
  105. from django.apps import apps
  106. urlpatterns = [
  107. # Your other URLs
  108. url(r'^', include(apps.get_app_config('myproject').urls[0])),
  109. ]
  110. All URLs containing ``/catalogue/`` previously are now displayed as ``/catalog/``.