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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 it's own URLs. And as
  18. it is a class, it can be overridden as 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. The root app pulls in the URLs from it's children. That means to add
  38. all Oscar URLs to your Django project, you only need to include the ``urls``
  39. property from the root app::
  40. # urls.py
  41. from oscar.app import application
  42. urlpatterns = [
  43. ... # Your other URLs
  44. url(r'', include(application.urls)),
  45. ]
  46. Changing sub app
  47. ----------------
  48. Sub-apps such as the ``catalogue`` app are loaded dynamically, just as most
  49. other classes in Oscar::
  50. # oscar/app.py
  51. class Shop(Application):
  52. name = None
  53. catalogue_app = get_class('catalogue.app', 'application')
  54. customer_app = get_class('customer.app', 'application')
  55. ...
  56. That means you just need to create another
  57. ``application`` instance. It will usually inherit from Oscar's version. Say
  58. you'd want to add another view to the promotions app. You only need to
  59. create a class called ``PromotionsApplication`` (and usually inherit from
  60. Oscar's version) and add your view::
  61. # yourproject/promotions/app.py
  62. from oscar.apps.promotions.app import PromotionsApplication as CorePromotionsApplication
  63. from .views import MyExtraView
  64. class PromotionsApplication(CorePromotionsApplication):
  65. extra_view = MyExtraView
  66. application = PromotionsApplication()
  67. Changing the root app
  68. ---------------------
  69. If you want to e.g. change the URL for the catalogue app from ``/catalogue``
  70. to ``/catalog``, you need to use a custom root app instance
  71. instead of Oscar's default instance. Hence, create a subclass of Oscar's main
  72. ``Application`` class and override the ``get_urls`` method::
  73. # myproject/app.py
  74. from oscar import app
  75. class MyShop(app.Shop):
  76. # Override get_urls method
  77. def get_urls(self):
  78. urlpatterns = [
  79. url(r'^catalog/', include(self.catalogue_app.urls)),
  80. ... # all the remaining URLs, removed for simplicity
  81. ]
  82. return urlpatterns
  83. application = MyShop()
  84. As the root app is hardcoded in your project's ``urls.py``, you need to modify
  85. it to use your new application instance instead of Oscar's default::
  86. # urls.py
  87. from myproject.app import application
  88. urlpatterns = [
  89. ... # Your other URLs
  90. url(r'', include(application.urls)),
  91. ]
  92. All URLs containing ``catalogue`` previously are now displayed as ``catalog``.