You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

how_to_change_a_url.rst 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 application class
  13. ---------------------
  14. Each Oscar app comes with an application instance which inherits from
  15. :class:`oscar.core.application.Application`. They're mainly used to gather
  16. URLs (with the correct permissions) for each Oscar app. This structure makes
  17. Oscar apps more modular as each app is responsible for its own URLs. And as
  18. it is a class, it can be overridden like any other Oscar class; hence making
  19. it straightforward to change URLs or add new views.
  20. Each app instance exposes a ``urls`` property, which is used to access the
  21. list of URLs of an app.
  22. The application tree
  23. --------------------
  24. Oscar's app instances are organised in a tree structure. The root application
  25. illustrates this nicely::
  26. # oscar/app.py
  27. class Shop(Application):
  28. name = None
  29. catalogue_app = get_class('catalogue.app', 'application')
  30. basket_app = get_class('basket.app', 'application')
  31. # ...
  32. def get_urls(self):
  33. urls = [
  34. url(r'^catalogue/', include(self.catalogue_app.urls)),
  35. url(r'^basket/', include(self.basket_app.urls)),
  36. # ...
  37. ]
  38. The root app pulls in the URLs from its children. That means to add
  39. all Oscar URLs to your Django project, you only need to include the ``urls``
  40. property from the root app::
  41. # urls.py
  42. from oscar.app import application
  43. urlpatterns = [
  44. # Your other URLs
  45. url(r'', include(application.urls)),
  46. ]
  47. Changing sub app
  48. ----------------
  49. Sub-apps such as the ``catalogue`` app are loaded dynamically, just as most
  50. other classes in Oscar::
  51. # oscar/app.py
  52. class Shop(Application):
  53. name = None
  54. catalogue_app = get_class('catalogue.app', 'application')
  55. customer_app = get_class('customer.app', 'application')
  56. # ...
  57. That means you just need to create another
  58. ``application`` instance. It will usually inherit from Oscar's version. Say
  59. you'd want to add another view to the promotions app. You only need to
  60. create a class called ``PromotionsApplication`` (and usually inherit from
  61. Oscar's version) and add your view::
  62. # yourproject/promotions/app.py
  63. from oscar.apps.promotions.app import PromotionsApplication as CorePromotionsApplication
  64. from .views import MyExtraView
  65. class PromotionsApplication(CorePromotionsApplication):
  66. extra_view = MyExtraView
  67. application = PromotionsApplication()
  68. Changing the root app
  69. ---------------------
  70. If you want to e.g. change the URL for the catalogue app from ``/catalogue``
  71. to ``/catalog``, you need to use a custom root app instance
  72. instead of Oscar's default instance. Hence, create a subclass of Oscar's main
  73. ``Application`` class and override the ``get_urls`` method::
  74. # myproject/app.py
  75. from oscar import app
  76. class MyShop(app.Shop):
  77. # Override get_urls method
  78. def get_urls(self):
  79. urlpatterns = [
  80. url(r'^catalog/', include(self.catalogue_app.urls)),
  81. # all the remaining URLs, removed for simplicity
  82. # ...
  83. ]
  84. return urlpatterns
  85. application = MyShop()
  86. As the root app is hardcoded in your project's ``urls.py``, you need to modify
  87. it to use your new application instance instead of Oscar's default::
  88. # urls.py
  89. from myproject.app import application
  90. urlpatterns = [
  91. # Your other URLs
  92. url(r'', include(application.urls)),
  93. ]
  94. All URLs containing ``catalogue`` previously are now displayed as ``catalog``.