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 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 by hand
  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. Settings
  34. --------
  35. Now edit your settings file ``frobshop.frobshop.settings.py`` to specify a
  36. database (we use SQLite for simplicity):
  37. .. code-block:: django
  38. DATABASES = {
  39. 'default': {
  40. 'ENGINE': 'django.db.backends.sqlite3',
  41. 'NAME': 'db.sqlite3',
  42. 'USER': '',
  43. 'PASSWORD': '',
  44. 'HOST': '',
  45. 'PORT': '',
  46. }
  47. }
  48. Now set ``TEMPLATE_CONTEXT_PROCESSORS`` to:
  49. .. code-block:: django
  50. TEMPLATE_CONTEXT_PROCESSORS = (
  51. "django.contrib.auth.context_processors.auth",
  52. "django.core.context_processors.request",
  53. "django.core.context_processors.debug",
  54. "django.core.context_processors.i18n",
  55. "django.core.context_processors.media",
  56. "django.core.context_processors.static",
  57. "django.core.context_processors.tz",
  58. "django.contrib.messages.context_processors.messages",
  59. 'oscar.apps.search.context_processors.search_form',
  60. 'oscar.apps.promotions.context_processors.promotions',
  61. 'oscar.apps.checkout.context_processors.checkout',
  62. 'oscar.apps.customer.notifications.context_processors.notifications',
  63. 'oscar.core.context_processors.metadata',
  64. )
  65. Next, modify ``INSTALLED_APPS`` to be a list, add ``South`` and ``compressor``
  66. and append Oscar's core apps:
  67. .. code-block:: django
  68. from oscar import get_core_apps
  69. INSTALLED_APPS = [
  70. 'django.contrib.auth',
  71. 'django.contrib.contenttypes',
  72. 'django.contrib.sessions',
  73. 'django.contrib.sites',
  74. 'django.contrib.messages',
  75. 'django.contrib.staticfiles',
  76. 'django.contrib.flatpages',
  77. ...
  78. 'south',
  79. 'compressor',
  80. ] + get_core_apps()
  81. SITE_ID = 1
  82. Note that Oscar requires ``django.contrib.flatpages`` which isn't
  83. included by default. ``flatpages`` also requires ``django.contrib.sites``,
  84. which won't be enabled by default when using Django 1.6 or upwards.
  85. Next, add ``oscar.apps.basket.middleware.BasketMiddleware``,
  86. ``django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`` to
  87. your ``MIDDLEWARE_CLASSES`` setting. It is also recommended to use
  88. ``django.middleware.transaction.TransactionMiddleware``:
  89. .. code-block:: django
  90. MIDDLEWARE_CLASSES = (
  91. ...
  92. 'oscar.apps.basket.middleware.BasketMiddleware',
  93. 'django.middleware.transaction.TransactionMiddleware', # recommended for oscar
  94. 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
  95. )
  96. More info about `django-flatpages installation`_ at the django-project website.
  97. .. _`django-flatpages installation`: https://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#installation
  98. .. tip::
  99. Oscar's default templates use django-compressor_ but it's optional really.
  100. You may decide to use your own templates that don't use compressor. Hence
  101. why it is not one of the 'core apps'.
  102. .. _django-compressor: https://github.com/jezdez/django_compressor
  103. Now set your auth backends to:
  104. .. code-block:: django
  105. AUTHENTICATION_BACKENDS = (
  106. 'oscar.apps.customer.auth_backends.Emailbackend',
  107. 'django.contrib.auth.backends.ModelBackend',
  108. )
  109. to allow customers to sign in using an email address rather than a username.
  110. Set ``MEDIA_ROOT`` and ``MEDIA_URL`` to your environment, and make sure the
  111. path in ``MEDIA_ROOT`` exists. An example from the Sandbox site:
  112. .. code-block:: django
  113. PROJECT_DIR = os.path.dirname(__file__)
  114. location = lambda x: os.path.join(
  115. os.path.dirname(os.path.realpath(__file__)), x)
  116. MEDIA_ROOT = location("public/media")
  117. MEDIA_URL = '/media/'
  118. Verify your ``staticfiles`` settings and ensure that files in ``MEDIA_ROOT``
  119. get served:
  120. * `staticfiles in Django 1.3 and 1.4 <https://docs.djangoproject.com/en/1.3/howto/static-files/#serving-other-directories>`_
  121. * `staticfiles in Django 1.5 <https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user>`_
  122. Modify your ``TEMPLATE_DIRS`` to include the main Oscar template directory:
  123. .. code-block:: django
  124. from oscar import OSCAR_MAIN_TEMPLATE_DIR
  125. TEMPLATE_DIRS = (
  126. location('templates'),
  127. OSCAR_MAIN_TEMPLATE_DIR,
  128. )
  129. Oscar currently uses Haystack for search so you need to specify:
  130. .. code-block:: django
  131. HAYSTACK_CONNECTIONS = {
  132. 'default': {
  133. 'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
  134. },
  135. }
  136. When moving towards production, you'll obviously need to switch to a real search
  137. backend.
  138. The last addition to the settings file is to import all of Oscar's default settings:
  139. .. code-block:: django
  140. from oscar.defaults import *
  141. URLs
  142. ----
  143. Alter your ``frobshop/urls.py`` to include Oscar's URLs:
  144. .. code-block:: django
  145. from django.conf.urls import patterns, include, url
  146. from oscar.app import application
  147. urlpatterns = patterns('',
  148. url(r'', include(application.urls))
  149. )
  150. Database
  151. --------
  152. Then create the database and the shop should be browsable:
  153. .. code-block:: bash
  154. $ python manage.py syncdb --noinput
  155. $ python manage.py migrate
  156. $ python manage.py runserver
  157. You should now have a running Oscar install that you can browse.
  158. Fixtures
  159. --------
  160. The default checkout process requires a shipping address with a country. Oscar
  161. uses a model for countries with flags that indicate which are valid shipping
  162. countries and so the ``address_country`` database table must be populated before
  163. a customer can check out.
  164. This is easily achieved using fixtures. Oscar ships with a ``countries.json``
  165. fixture that loads most countries from the `ISO 3166 standard`_. This can loaded
  166. via::
  167. $ python manage.py loaddata countries
  168. Note however that this file only sets the UK as a valid shipping country. If
  169. you want other countries to be available, it would make more sense to take a
  170. copy of Oscar's countries fixture and edit it as you see it before loading it.
  171. Further, a simple way of loading countries for your project is to use a `data
  172. migration`_.
  173. .. _`ISO 3166 standard`: http://en.wikipedia.org/wiki/ISO_3166
  174. .. _`data migration`: http://codeinthehole.com/writing/prefer-data-migrations-to-initial-data/
  175. Creating product classes and fulfillment partners
  176. -------------------------------------------------
  177. Every Oscar deployment needs at least one
  178. :class:`product class <oscar.apps.catalogue.abstract_models.AbstractProductClass>`
  179. and one
  180. :class:`fulfillment partner <oscar.apps.partner.abstract_models.AbstractPartner>`.
  181. These aren't created automatically as they're highly specific to the shop you
  182. want to build.
  183. The quickest way to set them up is to log into the Django admin
  184. interface at http://127.0.0.1:8000/admin/ and create instances of both there.
  185. For a deployment setup, we recommend creating them as `data migration`_.
  186. .. _data migration: http://codeinthehole.com/writing/prefer-data-migrations-to-initial-data/
  187. Defining the order pipeline
  188. ---------------------------
  189. The order management in Oscar relies on the order pipeline that
  190. defines all the statuses an order can have and the possible transitions
  191. for any given status. Statuses in Oscar are not just used for an order
  192. but are handled on the line level as well to be able to handle partial
  193. shipping of an order.
  194. The order status pipeline is different for every shop which means that
  195. changing it is fairly straightforward in Oscar. The pipeline is defined in
  196. your ``settings.py`` file using the ``OSCAR_ORDER_STATUS_PIPELINE`` setting.
  197. You also need to specify the initial status for an order and a line item in
  198. ``OSCAR_INITIAL_ORDER_STATUS`` and ``OSCAR_INITIAL_LINE_STATUS``
  199. respectively.
  200. To give you an idea of what an order pipeline might look like take a look
  201. at the Oscar sandbox settings:
  202. .. code-block:: django
  203. OSCAR_INITIAL_ORDER_STATUS = 'Pending'
  204. OSCAR_INITIAL_LINE_STATUS = 'Pending'
  205. OSCAR_ORDER_STATUS_PIPELINE = {
  206. 'Pending': ('Being processed', 'Cancelled',),
  207. 'Being processed': ('Processed', 'Cancelled',),
  208. 'Cancelled': (),
  209. }
  210. Defining the order status pipeline is simply a dictionary of where each
  211. status is given as a key. Possible transitions into other statuses can be
  212. specified as an iterable of status names. An empty iterable defines an
  213. end point in the pipeline.
  214. With these three settings defined in your project you'll be able to see
  215. the different statuses in the order management dashboard.
  216. Next steps
  217. ==========
  218. The next step is to implement the business logic of your domain on top of
  219. Oscar. The fun part.