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.4KB

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