您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

how_to_customise_an_app.rst 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. =======================
  2. How to customise an app
  3. =======================
  4. A core part of how oscar can be customised is to create a local version of one
  5. of oscar's apps so that it can be modified and extended. Creating a local
  6. version of an app allows customisation of any of the classes within the
  7. corresponding app in oscar.
  8. The way this is done involves a few steps, which are detailed here.
  9. Method
  10. ======
  11. 1. Create an app within your project with the same "app label" as an app in oscar. Eg,
  12. to create a local version of ``oscar.apps.order``, create something like ``myproject.order``.
  13. 2. Ensure the ``models.py`` in your local app imports all the models from oscar's version::
  14. # models.py
  15. from oscar.apps.order.models import *
  16. 3. Replace oscar's version of the app with your new version in ``INSTALLED_APPS``.
  17. Worked example
  18. ==============
  19. Suppose you want to modify the homepage view class, which by default is defined in
  20. ``oscar.apps.promotions.views.HomeView``. This view is bound to a URL within the
  21. ``PromotionsApplication`` class in ``oscar.apps.promotions.app`` - hence we need to
  22. override this application class to be able to use a different view.
  23. By default, your base ``urls.py`` should include oscar's URLs as so::
  24. # urls.py
  25. from oscar.app import application
  26. urlpatterns = patterns('',
  27. ...
  28. (r'', include(application.urls)),
  29. )
  30. To get control over the mapping between URLs and views, you need to use a local
  31. ``application`` instance, that (optionally) subclasses oscar's. Hence, create
  32. ``myproject/app.py`` with contents::
  33. # myproject/app.py
  34. from oscar.app import Shop
  35. class BaseApplication(Shop):
  36. pass
  37. application = BaseApplication()
  38. No customisation for now, that will come later, but you now have control over which
  39. URLs and view functions are used.
  40. Now hook this up in your ``urls.py``::
  41. # urls.py
  42. from myproject.app import application
  43. urlpatterns = patterns('',
  44. ...
  45. (r'', include(application.urls)),
  46. )
  47. The next step is to create a local app with the same name as the app you want to override::
  48. mkdir myproject/promotions
  49. touch myproject/promotions/__init__.py
  50. touch myproject/promotions/models.py
  51. The ``models.py`` file should import all models from the oscar app being overridden::
  52. # myproject/promotions/models.py
  53. from oscar.apps.promotions.models import *
  54. Now replace ``oscar.apps.promotions`` with ``myproject.promotions`` in the ``INSTALLED_APPS``
  55. setting in your settings file.
  56. Now create a new homepage view class in ``myproject.promotions.views`` - you can subclass
  57. oscar's view if you like::
  58. from oscar.apps.promotions.views import HomeView as CoreHomeView
  59. class HomeView(CoreHomeView):
  60. template_name = 'promotions/new-homeview.html'
  61. In this example, we set a new template location but it's possible to customise the view
  62. in any imaginable way.
  63. Next, create a new ``app.py`` for your local promotions app which maps your new ``HomeView``
  64. class to the homepage URL::
  65. # myproject/promotions/app.py
  66. from oscar.apps.promotions import PromotionsApplication as CorePromotionsApplication
  67. from myproject.promotions.views import HomeView
  68. class PromotionsApplication(CorePromotionsApplication):
  69. home_view = HomeView
  70. application = PromotionsApplication()
  71. Finally, hook up the new view to the homepage URL::
  72. # myproject/app.py
  73. from oscar.app import Shop
  74. from myproject.promotions.app import application as promotions_app
  75. class BaseApplication(Shop):
  76. promotions_app = promotions_app
  77. Quite long-winded, but once this step is done, you have lots of freedom to customise
  78. the app in question.
  79. Other points of note
  80. --------------------
  81. One pain point with replacing one of oscar's apps with a local one in ``INSTALLED_APPS`` is
  82. that template tags are lost from the original app and need to be manually imported. This can be
  83. done by creating a local version of the template tags files::
  84. mkdir myproject/templatetags
  85. and importing the tags from oscar's corresponding file::
  86. # myproject/promotions/templatetags/promotion_tags.py
  87. from oscar.apps.promotions.templatetags.promotion_tags import *
  88. This isn't great but we haven't found a better way as of yet.