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.

getting_started.rst 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. ======================
  2. Building your own shop
  3. ======================
  4. For simplicity, let's assume you're building a new e-commerce project from
  5. scratch and have decided to use Oscar. Let's call this shop 'frobshop'
  6. .. tip::
  7. You can always review the set-up of the
  8. :doc:`Sandbox site <sandbox>` in case you have trouble with
  9. the below instructions.
  10. Install Oscar and its dependencies
  11. ==================================
  12. Install Oscar (which will install Django as a dependency), then create the
  13. project:
  14. .. code-block:: bash
  15. $ mkvirtualenv oscar
  16. $ pip install django-oscar
  17. $ django-admin.py startproject frobshop
  18. If you do not have mkvirtualenv, then replace that line with::
  19. $ virtualenv oscar
  20. $ . ./oscar/bin/activate
  21. (oscar) $
  22. This will create a folder ``frobshop`` for your project. It is highly
  23. recommended to install Oscar in a virtualenv.
  24. .. attention::
  25. Please ensure that ``pillow``, a fork of the the Python Imaging Library
  26. (PIL), gets installed with JPEG support. Supported formats are printed
  27. when ``pillow`` is first installed.
  28. Instructions_ on how to get JPEG support are highly platform specific,
  29. but guides for ``PIL`` should work for ``pillow`` as well. Generally
  30. speaking, you need to ensure that ``libjpeg-dev`` is installed and found
  31. during installation.
  32. .. _Instructions: http://www.google.com/search?q=install+pil+with+jpeg+support
  33. Django settings
  34. ===============
  35. Edit your settings file ``frobshop.frobshop.settings.py`` to specify
  36. ``TEMPLATE_CONTEXT_PROCESSORS``:
  37. .. code-block:: django
  38. TEMPLATE_CONTEXT_PROCESSORS = (
  39. "django.contrib.auth.context_processors.auth",
  40. "django.core.context_processors.request",
  41. "django.core.context_processors.debug",
  42. "django.core.context_processors.i18n",
  43. "django.core.context_processors.media",
  44. "django.core.context_processors.static",
  45. "django.core.context_processors.tz",
  46. "django.contrib.messages.context_processors.messages",
  47. 'oscar.apps.search.context_processors.search_form',
  48. 'oscar.apps.promotions.context_processors.promotions',
  49. 'oscar.apps.checkout.context_processors.checkout',
  50. 'oscar.apps.customer.notifications.context_processors.notifications',
  51. 'oscar.core.context_processors.metadata',
  52. )
  53. Next, modify ``INSTALLED_APPS`` to be a list, add ``South`` and ``compressor``
  54. and append Oscar's core apps:
  55. .. code-block:: django
  56. from oscar import get_core_apps
  57. INSTALLED_APPS = [
  58. 'django.contrib.auth',
  59. 'django.contrib.contenttypes',
  60. 'django.contrib.sessions',
  61. 'django.contrib.sites',
  62. 'django.contrib.messages',
  63. 'django.contrib.staticfiles',
  64. 'django.contrib.flatpages',
  65. ...
  66. 'south',
  67. 'compressor',
  68. ] + get_core_apps()
  69. SITE_ID = 1
  70. Note that Oscar requires ``django.contrib.flatpages`` which isn't
  71. included by default. ``flatpages`` also requires ``django.contrib.sites``,
  72. which won't be enabled by default when using Django 1.6 or upwards.
  73. More info about installing ``flatpages`` is in the `Django docs`_.
  74. .. _`Django docs`: https://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#installation
  75. .. tip::
  76. Oscar's default templates use django-compressor_ but it's optional really.
  77. You may decide to use your own templates that don't use compressor. Hence
  78. why it is not one of the 'core apps'.
  79. .. _django-compressor: https://github.com/jezdez/django_compressor
  80. Next, add ``oscar.apps.basket.middleware.BasketMiddleware`` and
  81. ``django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`` to
  82. your ``MIDDLEWARE_CLASSES`` setting. If you're running on Django 1.5, it is
  83. also recommended to use ``django.middleware.transaction.TransactionMiddleware``:
  84. .. code-block:: django
  85. MIDDLEWARE_CLASSES = (
  86. ...
  87. 'oscar.apps.basket.middleware.BasketMiddleware',
  88. 'django.middleware.transaction.TransactionMiddleware', # Django 1.5 only
  89. 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
  90. )
  91. If you're running Django 1.6 or above, you should enable ``ATOMIC_REQUESTS``
  92. instead (see database settings above).
  93. Set your auth backends to:
  94. .. code-block:: django
  95. AUTHENTICATION_BACKENDS = (
  96. 'oscar.apps.customer.auth_backends.Emailbackend',
  97. 'django.contrib.auth.backends.ModelBackend',
  98. )
  99. to allow customers to sign in using an email address rather than a username.
  100. Ensure that your media and static files are `configured correctly`_. This means
  101. at the least setting ``MEDIA_URL`` and ``STATIC_URL``. If you're serving files
  102. locally, you'll also need to set ``MEDIA_ROOT`` and ``STATIC_ROOT``.
  103. Check out the `sandbox settings`_ for a working example. If you're serving
  104. files from a remote storage (e.g. Amazon S3), you must manually copy a
  105. :ref:`"Image not found" image <missing-image-label>` into ``MEDIA_ROOT``.
  106. .. _`configured correctly`: https://docs.djangoproject.com/en/1.7/howto/static-files/
  107. .. _sandbox settings: https://github.com/tangentlabs/django-oscar/blob/3a5160a86c9b14c940c76a224a28cd37dd29f7f1/sites/sandbox/settings.py#L99
  108. Modify your ``TEMPLATE_DIRS`` to include the main Oscar template directory:
  109. .. code-block:: django
  110. from oscar import OSCAR_MAIN_TEMPLATE_DIR
  111. TEMPLATE_DIRS = (
  112. location('templates'),
  113. OSCAR_MAIN_TEMPLATE_DIR,
  114. )
  115. The last addition to the settings file is to import all of Oscar's default settings:
  116. .. code-block:: django
  117. from oscar.defaults import *
  118. URLs
  119. ====
  120. Alter your ``frobshop/urls.py`` to include Oscar's URLs. If you have more than
  121. one language set your Django settings for ``LANGUAGES``, you will also need to
  122. include Django's i18n URLs:
  123. .. code-block:: django
  124. from django.conf.urls import include, url
  125. from oscar.app import application
  126. urlpatterns = [
  127. url(r'^i18n/', include('django.conf.urls.i18n')),
  128. url(r'', include(application.urls))
  129. ]
  130. Search backend
  131. ==============
  132. If you're happy with basic search for now, you can just use Haystack's simple
  133. backend:
  134. .. code-block:: django
  135. HAYSTACK_CONNECTIONS = {
  136. 'default': {
  137. 'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
  138. },
  139. }
  140. Oscar uses Haystack to abstract away from different search backends.
  141. Unfortunately, writing backend-agnostic code is nonetheless hard and
  142. Apache Solr is currently the only supported production-grade backend. Your
  143. Haystack config could look something like this:
  144. .. code-block:: django
  145. HAYSTACK_CONNECTIONS = {
  146. 'default': {
  147. 'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
  148. 'URL': 'http://127.0.0.1:8983/solr',
  149. 'INCLUDE_SPELLING': True,
  150. },
  151. }
  152. Oscar includes a sample schema to get started with Solr. More information can
  153. be found in the
  154. :doc:`recipe on getting Solr up and running</howto/how_to_setup_solr>`.
  155. Database
  156. ========
  157. Check your database settings. A quick way to get started is to use SQLite:
  158. .. code-block:: django
  159. DATABASES = {
  160. 'default': {
  161. 'ENGINE': 'django.db.backends.sqlite3',
  162. 'NAME': 'db.sqlite3',
  163. 'USER': '',
  164. 'PASSWORD': '',
  165. 'HOST': '',
  166. 'PORT': '',
  167. 'ATOMIC_REQUESTS': True, # Django 1.6+
  168. }
  169. }
  170. Note that we recommend using ``ATOMIC_REQUESTS`` to tie transactions to
  171. requests.
  172. Then create the database and the shop should be browsable:
  173. .. code-block:: bash
  174. $ python manage.py syncdb --noinput
  175. $ python manage.py migrate
  176. $ python manage.py runserver
  177. You should now have an empty, but running Oscar install that you can browse at
  178. http://localhost:8000.
  179. Fixtures
  180. ========
  181. The default checkout process requires a shipping address with a country. Oscar
  182. uses a model for countries with flags that indicate which are valid shipping
  183. countries and so the ``address_country`` database table must be populated before
  184. a customer can check out.
  185. This is easily achieved using fixtures. Oscar ships with a ``countries.json``
  186. fixture that loads most countries from the `ISO 3166 standard`_. This can loaded
  187. via::
  188. $ python manage.py loaddata countries
  189. Note however that this file only sets the UK as a valid shipping country. If
  190. you want other countries to be available, it would make more sense to take a
  191. copy of Oscar's countries fixture and edit it as you see it before loading it.
  192. Further, a simple way of loading countries for your project is to use a `data
  193. migration`_.
  194. .. _`ISO 3166 standard`: http://en.wikipedia.org/wiki/ISO_3166
  195. .. _`data migration`: http://codeinthehole.com/writing/prefer-data-migrations-to-initial-data/
  196. Creating product classes and fulfillment partners
  197. =================================================
  198. Every Oscar deployment needs at least one
  199. :class:`product class <oscar.apps.catalogue.abstract_models.AbstractProductClass>`
  200. and one
  201. :class:`fulfillment partner <oscar.apps.partner.abstract_models.AbstractPartner>`.
  202. These aren't created automatically as they're highly specific to the shop you
  203. want to build.
  204. The quickest way to set them up is to log into the Django admin
  205. interface at http://127.0.0.1:8000/admin/ and create instances of both there.
  206. For a deployment setup, we recommend creating them as `data migration`_.
  207. Defining the order pipeline
  208. ===========================
  209. The order management in Oscar relies on the order pipeline that
  210. defines all the statuses an order can have and the possible transitions
  211. for any given status. Statuses in Oscar are not just used for an order
  212. but are handled on the line level as well to be able to handle partial
  213. shipping of an order.
  214. The order status pipeline is different for every shop which means that
  215. changing it is fairly straightforward in Oscar. The pipeline is defined in
  216. your ``settings.py`` file using the ``OSCAR_ORDER_STATUS_PIPELINE`` setting.
  217. You also need to specify the initial status for an order and a line item in
  218. ``OSCAR_INITIAL_ORDER_STATUS`` and ``OSCAR_INITIAL_LINE_STATUS``
  219. respectively.
  220. To give you an idea of what an order pipeline might look like take a look
  221. at the Oscar sandbox settings:
  222. .. code-block:: django
  223. OSCAR_INITIAL_ORDER_STATUS = 'Pending'
  224. OSCAR_INITIAL_LINE_STATUS = 'Pending'
  225. OSCAR_ORDER_STATUS_PIPELINE = {
  226. 'Pending': ('Being processed', 'Cancelled',),
  227. 'Being processed': ('Processed', 'Cancelled',),
  228. 'Cancelled': (),
  229. }
  230. Defining the order status pipeline is simply a dictionary of where each
  231. status is given as a key. Possible transitions into other statuses can be
  232. specified as an iterable of status names. An empty iterable defines an
  233. end point in the pipeline.
  234. With these three settings defined in your project you'll be able to see
  235. the different statuses in the order management dashboard.
  236. Next steps
  237. ==========
  238. The next step is to implement the business logic of your domain on top of
  239. Oscar. The fun part.