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_handle_statics.rst 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. .. spelling::
  2. NGINX
  3. Gulp
  4. ================================
  5. How to change Oscar's appearance
  6. ================================
  7. This is a guide for Front-End Developers (FEDs) working on Oscar projects, not
  8. on Oscar itself. It is written with Tangent's FED team in mind but should be
  9. more generally useful for anyone trying to customise Oscar and looking for the
  10. right approach.
  11. Overview
  12. ========
  13. Oscar ships with a set of HTML templates and a collection of static files
  14. (e.g. images and Javascript). Oscar's default CSS is generated from SASS
  15. files.
  16. Templates
  17. ---------
  18. Oscar's default templates use the mark-up conventions from the Bootstrap
  19. project. Classes for styling should be separate from classes used for
  20. Javascript. The latter must be prefixed with ``js-``, and using data attributes
  21. is often preferable.
  22. Frontend vs. Dashboard
  23. ----------------------
  24. The frontend and dashboard are intentionally kept separate. They incidentally
  25. both use Bootstrap and Font Awesome, but may be updated individually.
  26. For the frontend, ``styles.scss`` imports Bootstrap and Font Awesome SASS
  27. stylesheets, and ties them together with Oscar-specific styling.
  28. For the dashboard, ``dashboard.scss`` also imports Bootstrap and Font Awesome
  29. SASS stylesheets, and adds Oscar-specific customisations.
  30. SCSS/CSS
  31. --------
  32. CSS files served to the browser are compiled from their SASS sources. For
  33. local development, :command:`npm run watch` will watch for local changes to SASS files and
  34. automatically rebuild the compiled CSS.
  35. Use the command :command:`make assets` to compile assets manually.
  36. Javascript
  37. ----------
  38. Oscar uses Javascript for progressive enhancements. This guide used to document
  39. exact versions, but quickly became outdated. It is recommended to inspect
  40. ``layout.html`` and ``dashboard/layout.html`` for what is currently included.
  41. Customisation
  42. =============
  43. Customising templates
  44. ---------------------
  45. Oscar ships with a complete set of templates (in ``oscar/templates``). These
  46. will be available to an Oscar project but can be overridden or modified.
  47. The templates use Bootstrap conventions for class names and mark-up.
  48. There is a separate recipe on how to do this.
  49. Customising statics
  50. -------------------
  51. Oscar's static files are stored in ``oscar/static``. When a Django site is
  52. deployed, the ``collectstatic`` command is run which collects static files from
  53. all installed apps and puts them in a single location (called the
  54. ``STATIC_ROOT``). It is common for a separate HTTP server (like NGINX) to be
  55. used to serve these files, setting its document root to ``STATIC_ROOT``.
  56. For an individual project, you may want to override Oscar's static files. The
  57. best way to do this is to have a statics folder within your project and to add
  58. it to the ``STATICFILES_DIRS`` setting. Then, any files which match the same
  59. path as files in Oscar will be served from your local statics folder instead.
  60. For instance, if you want to use a local version of ``oscar/css/styles.css``,
  61. your could create a file::
  62. yourproject/
  63. static/
  64. oscar/
  65. css/
  66. styles.css
  67. and this would override Oscar's equivalent file.
  68. To make things easier, Oscar ships with a management command for creating a copy
  69. of all of its static files. This breaks the link with Oscar's static files and
  70. means everything is within the control of the project. Run it as follows::
  71. # with default "target_path" of "static"
  72. ./manage.py oscar_fork_statics
  73. or
  74. # with custom "target_path"
  75. ./manage.py oscar_fork_statics /path/to/static/directory/
  76. This is the recommended approach for non-trivial projects.
  77. Another option is simply to ignore all of Oscar's CSS and write your own from
  78. scratch. To do this, you simply need to adjust the layout templates to include
  79. your own CSS instead of Oscar's. For instance, you might override ``oscar/layout.html``
  80. and replace the ``styles`` block::
  81. # project/oscar/layout.html
  82. {% extends "oscar/layout.html" %}
  83. {% load static %}
  84. {% block styles %}
  85. <link rel="stylesheet" type="text/css" href="{% static 'myproject/styles.css' %}" />
  86. {% endblock %}