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

how_to_handle_statics.rst 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ================================
  2. How to change Oscar's appearance
  3. ================================
  4. This is a guide for Front-End Developers (FEDs) working on Oscar projects, not
  5. on Oscar itself. It is written with Tangent's FED team in mind but should be
  6. more generally useful for anyone trying to customise Oscar and looking for the
  7. right approach.
  8. Overview
  9. ========
  10. Oscar ships with a set of HTML templates and a collection of static files
  11. (eg images, javascript). Oscar's default CSS is generated from LESS
  12. files.
  13. Templates
  14. ---------
  15. Oscar's default templates use the mark-up conventions from the Bootstrap
  16. project. Classes for styling should be separate from classes used for
  17. Javascript. The latter must be prefixed with ``js-``, and using data attributes
  18. is often preferable.
  19. Frontend vs. Dashboard
  20. ----------------------
  21. The frontend and dashboard are intentionally kept very separate. They
  22. incidentally both use Bootstrap, but may be updated individually.
  23. The frontend is based on Bootstrap's LESS files and ties it together with
  24. Oscar-specific styling in ``styles.less``.
  25. On the other hand, ``dashboard.less`` just contains a few customisations that
  26. are included alongside a copy of stock Bootstrap CSS - and at the time of
  27. writing, using a different Bootstrap version.
  28. LESS/CSS
  29. --------
  30. By default, CSS files compiled from their LESS sources are used rather than the
  31. LESS ones. To use Less directly, set ``USE_LESS = True`` in your settings file.
  32. You will also need to ensure that the ``lessc`` executable is installed and is
  33. configured using a setting like::
  34. COMPRESS_PRECOMPILERS = (
  35. ('text/less', 'lessc {infile} {outfile}'),
  36. )
  37. A few other CSS files are used to provide styles for javascript libraries.
  38. Using offline compression
  39. ~~~~~~~~~~~~~~~~~~~~~~~~~
  40. Django compressor also provides a way of running offline compression which can
  41. be used during deployment to automatically generate CSS files from your LESS
  42. files. To make sure that compressor is obeying the ``USE_LESS`` setting and
  43. is not trying to compress CSS files that are not available, the setting has to
  44. be passed into the ``COMPRESS_OFFLINE_CONTEXT``. You should add something like
  45. this to your settings file::
  46. COMPRESS_OFFLINE_CONTEXT = {
  47. # this is the only default value from compressor itself
  48. 'STATIC_URL': STATIC_URL,
  49. 'use_less': USE_LESS,
  50. }
  51. Javascript
  52. ----------
  53. Oscar uses javascript for progressive enhancements. This guide used to document
  54. exact versions, but quickly became outdated. It is recommended to inspect
  55. ``layout.html`` and ``dashboard/layout.html`` for what is currently included.
  56. Customisation
  57. =============
  58. Customising templates
  59. ---------------------
  60. Oscar ships with a complete set of templates (in ``oscar/templates``). These
  61. will be available to an Oscar project but can be overridden or modified.
  62. The templates use Bootstrap conventions for class names and mark-up.
  63. There is a separate recipe on how to do this.
  64. Customising statics
  65. -------------------
  66. Oscar's static files are stored in ``oscar/static``. When a Django site is
  67. deployed, the ``collectstatic`` command is run which collects static files from
  68. all installed apps and puts them in a single location (called the
  69. ``STATIC_ROOT``). It is common for a separate HTTP server (like nginx) to be
  70. used to serve these files, setting its document root to ``STATIC_ROOT``.
  71. For an individual project, you may want to override Oscar's static files. The
  72. best way to do this is to have a statics folder within your project and to add
  73. it to the ``STATICFILES_DIRS`` setting. Then, any files which match the same
  74. path as files in Oscar will be served from your local statics folder instead.
  75. For instance, if you want to use a local version of ``oscar/css/styles.css``,
  76. your could create a file::
  77. yourproject/
  78. static/
  79. oscar/
  80. css/
  81. styles.css
  82. and this would override Oscar's equivalent file.
  83. To make things easier, Oscar ships with a management command for creating a copy
  84. of all of its static files. This breaks the link with Oscar's static files and
  85. means everything is within the control of the project. Run it as follows::
  86. ./manage.py oscar_fork_statics
  87. This is the recommended approach for non-trivial projects.
  88. Another option is simply to ignore all of Oscar's CSS and write your own from
  89. scratch. To do this, you simply need to adjust the layout templates to include
  90. your own CSS instead of Oscar's. For instance, you might override ``base.html``
  91. and replace the 'less' block::
  92. # project/base.html
  93. {% block less %}
  94. <link rel="stylesheet" type="text/less" href="{{ STATIC_URL }}myproject/less/styles.less" />
  95. {% endblock %}