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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. .. spelling::
  2. Solr
  3. ======================
  4. Building your own shop
  5. ======================
  6. For simplicity, let's assume you're building a new e-commerce project from
  7. scratch and have decided to use Oscar. Let's call this project ``frobshop``
  8. .. tip::
  9. You can always review the set-up of the
  10. :doc:`Sandbox site <sandbox>` in case you have trouble with
  11. the below instructions.
  12. Install Oscar and its dependencies
  13. ==================================
  14. Install Oscar (which will install Django as a dependency), then create the
  15. project:
  16. .. code-block:: bash
  17. $ mkvirtualenv oscar
  18. $ pip install django-oscar
  19. $ django-admin.py startproject frobshop
  20. If you do not have :command:`mkvirtualenv`, then replace that line with::
  21. $ virtualenv oscar
  22. $ . ./oscar/bin/activate
  23. (oscar) $
  24. This will create a folder ``frobshop`` for your project. It is highly
  25. recommended to install Oscar in a virtualenv.
  26. .. attention::
  27. Please ensure that ``pillow``, a fork of the the Python Imaging Library
  28. (PIL), gets installed with JPEG support. Supported formats are printed
  29. when ``pillow`` is first installed.
  30. Instructions_ on how to get JPEG support are highly platform specific,
  31. but guides for ``PIL`` should work for ``pillow`` as well. Generally
  32. speaking, you need to ensure that ``libjpeg-dev`` is installed and found
  33. during installation.
  34. .. _Instructions: http://www.google.com/search?q=install+pil+with+jpeg+support
  35. Django settings
  36. ===============
  37. First, edit your settings file ``frobshop.frobshop.settings.py`` to import all of Oscar's default settings.
  38. .. code-block:: django
  39. from oscar.defaults import *
  40. Now add Oscar's context processors to the template settings, listed below:
  41. .. code-block:: django
  42. 'oscar.apps.search.context_processors.search_form',
  43. 'oscar.apps.checkout.context_processors.checkout',
  44. 'oscar.apps.communication.notifications.context_processors.notifications',
  45. 'oscar.core.context_processors.metadata',
  46. Next, modify ``INSTALLED_APPS`` to be a list, and add ``django.contrib.sites``,
  47. ``django.contrib.flatpages``, Oscar's core apps, and third-party apps that Oscar
  48. depends on. Also set ``SITE_ID``:
  49. .. code-block:: django
  50. INSTALLED_APPS = [
  51. 'django.contrib.admin',
  52. 'django.contrib.auth',
  53. 'django.contrib.contenttypes',
  54. 'django.contrib.sessions',
  55. 'django.contrib.messages',
  56. 'django.contrib.staticfiles',
  57. 'django.contrib.sites',
  58. 'django.contrib.flatpages',
  59. 'oscar.config.Shop',
  60. 'oscar.apps.analytics.apps.AnalyticsConfig',
  61. 'oscar.apps.checkout.apps.CheckoutConfig',
  62. 'oscar.apps.address.apps.AddressConfig',
  63. 'oscar.apps.shipping.apps.ShippingConfig',
  64. 'oscar.apps.catalogue.apps.CatalogueConfig',
  65. 'oscar.apps.catalogue.reviews.apps.CatalogueReviewsConfig',
  66. 'oscar.apps.communication.apps.CommunicationConfig',
  67. 'oscar.apps.partner.apps.PartnerConfig',
  68. 'oscar.apps.basket.apps.BasketConfig',
  69. 'oscar.apps.payment.apps.PaymentConfig',
  70. 'oscar.apps.offer.apps.OfferConfig',
  71. 'oscar.apps.order.apps.OrderConfig',
  72. 'oscar.apps.customer.apps.CustomerConfig',
  73. 'oscar.apps.search.apps.SearchConfig',
  74. 'oscar.apps.voucher.apps.VoucherConfig',
  75. 'oscar.apps.wishlists.apps.WishlistsConfig',
  76. 'oscar.apps.dashboard.apps.DashboardConfig',
  77. 'oscar.apps.dashboard.reports.apps.ReportsDashboardConfig',
  78. 'oscar.apps.dashboard.users.apps.UsersDashboardConfig',
  79. 'oscar.apps.dashboard.orders.apps.OrdersDashboardConfig',
  80. 'oscar.apps.dashboard.catalogue.apps.CatalogueDashboardConfig',
  81. 'oscar.apps.dashboard.offers.apps.OffersDashboardConfig',
  82. 'oscar.apps.dashboard.partners.apps.PartnersDashboardConfig',
  83. 'oscar.apps.dashboard.pages.apps.PagesDashboardConfig',
  84. 'oscar.apps.dashboard.ranges.apps.RangesDashboardConfig',
  85. 'oscar.apps.dashboard.reviews.apps.ReviewsDashboardConfig',
  86. 'oscar.apps.dashboard.vouchers.apps.VouchersDashboardConfig',
  87. 'oscar.apps.dashboard.communications.apps.CommunicationsDashboardConfig',
  88. 'oscar.apps.dashboard.shipping.apps.ShippingDashboardConfig',
  89. # 3rd-party apps that oscar depends on
  90. 'widget_tweaks',
  91. 'haystack',
  92. 'treebeard',
  93. 'sorl.thumbnail',
  94. 'django_tables2',
  95. ]
  96. SITE_ID = 1
  97. Note that Oscar requires ``django.contrib.flatpages`` which isn't
  98. included by default. ``flatpages`` also requires ``django.contrib.sites``.
  99. More info about installing ``flatpages`` is in the `Django docs`_.
  100. .. _`Django docs`: https://docs.djangoproject.com/en/stable/ref/contrib/flatpages/#installation
  101. .. tip::
  102. Oscar's default templates use django-widget-tweaks_ but it's
  103. optional really. You may decide to use your own templates that
  104. don't use either.
  105. .. _django-widget-tweaks: https://github.com/kmike/django-widget-tweaks
  106. Next, add ``oscar.apps.basket.middleware.BasketMiddleware`` and
  107. ``django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`` to
  108. your ``MIDDLEWARE`` setting.
  109. .. code-block:: django
  110. MIDDLEWARE = (
  111. ...
  112. 'oscar.apps.basket.middleware.BasketMiddleware',
  113. 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
  114. )
  115. Set your authentication backends to:
  116. .. code-block:: django
  117. AUTHENTICATION_BACKENDS = (
  118. 'oscar.apps.customer.auth_backends.EmailBackend',
  119. 'django.contrib.auth.backends.ModelBackend',
  120. )
  121. to allow customers to sign in using an email address rather than a username.
  122. Ensure that your media and static files are `configured correctly`_. This means
  123. at the least setting ``MEDIA_URL`` and ``STATIC_URL``. If you're serving files
  124. locally, you'll also need to set ``MEDIA_ROOT`` and ``STATIC_ROOT``.
  125. Check out the `sandbox settings`_ for a working example. If you're serving
  126. files from a remote storage (e.g. Amazon S3), you must manually copy a
  127. :ref:`"Image not found" image <missing-image-label>` into ``MEDIA_ROOT``.
  128. .. _`configured correctly`: https://docs.djangoproject.com/en/stable/howto/static-files/
  129. .. _sandbox settings: https://github.com/django-oscar/django-oscar/blob/master/sandbox/settings.py#L102
  130. URLs
  131. ====
  132. Alter your ``frobshop/urls.py`` to include Oscar's URLs. You can also include
  133. the Django admin for debugging purposes. But please note that Oscar makes no
  134. attempts at having that be a workable interface; admin integration exists
  135. to ease the life of developers.
  136. If you have more than one language set your Django settings for ``LANGUAGES``,
  137. you will also need to include Django's i18n URLs:
  138. .. code-block:: django
  139. from django.apps import apps
  140. from django.conf.urls import include, url # < Django-2.0
  141. # from django.urls import include, path # > Django-2.0
  142. from django.contrib import admin
  143. urlpatterns = [
  144. url(r'^i18n/', include('django.conf.urls.i18n')),
  145. # path('i18n/', include('django.conf.urls.i18n')), # > Django-2.0
  146. # The Django admin is not officially supported; expect breakage.
  147. # Nonetheless, it's often useful for debugging.
  148. url(r'^admin/', admin.site.urls),
  149. # path('admin/', admin.site.urls), # > Django-2.0
  150. url(r'^', include(apps.get_app_config('oscar').urls[0])),
  151. # path('', include(apps.get_app_config('oscar').urls[0])), # > Django-2.0
  152. ]
  153. Search backend
  154. ==============
  155. If you're happy with basic search for now, you can just add Haystack's simple
  156. backend to the ``HAYSTACK_CONNECTIONS`` option in your Django settings:
  157. .. code-block:: django
  158. HAYSTACK_CONNECTIONS = {
  159. 'default': {
  160. 'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
  161. },
  162. }
  163. Oscar uses Haystack to abstract away from different search backends.
  164. Unfortunately, writing backend-agnostic code is nonetheless hard and
  165. Apache Solr is currently the only supported production-grade backend. Your
  166. Haystack config could look something like this:
  167. .. code-block:: django
  168. HAYSTACK_CONNECTIONS = {
  169. 'default': {
  170. 'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
  171. 'URL': 'http://127.0.0.1:8983/solr',
  172. 'INCLUDE_SPELLING': True,
  173. },
  174. }
  175. Oscar includes a sample schema to get started with Solr. More information can
  176. be found in the
  177. :doc:`recipe on getting Solr up and running</howto/how_to_setup_solr>`.
  178. Database
  179. ========
  180. Check your database settings. A quick way to get started is to use SQLite:
  181. .. code-block:: django
  182. DATABASES = {
  183. 'default': {
  184. 'ENGINE': 'django.db.backends.sqlite3',
  185. 'NAME': 'db.sqlite3',
  186. 'USER': '',
  187. 'PASSWORD': '',
  188. 'HOST': '',
  189. 'PORT': '',
  190. 'ATOMIC_REQUESTS': True,
  191. }
  192. }
  193. Note that we recommend using ``ATOMIC_REQUESTS`` to tie transactions to
  194. requests.
  195. Create database
  196. ---------------
  197. Oscar ships with migrations. Django's migration framework will detect them
  198. automatically and will do the right thing.
  199. Create the database and the shop should be browsable:
  200. .. code-block:: bash
  201. $ python manage.py migrate
  202. $ python manage.py runserver
  203. You should now have an empty, but running Oscar install that you can browse at
  204. http://localhost:8000.
  205. Initial data
  206. ============
  207. The default checkout process requires a shipping address with a country. Oscar
  208. uses a model for countries with flags that indicate which are valid shipping
  209. countries and so the ``country`` database table must be populated before
  210. a customer can check out.
  211. The easiest way to achieve this is to use country data from the `pycountry`_
  212. package. Oscar ships with a management command to parse that data:
  213. .. code-block:: bash
  214. $ pip install pycountry
  215. [...]
  216. $ python manage.py oscar_populate_countries
  217. By default, this command will mark all countries as a shipping country. Call
  218. it with the ``--no-shipping`` option to prevent that. You then need to
  219. manually mark at least one country as a shipping country.
  220. .. _pycountry: https://pypi.python.org/pypi/pycountry
  221. Creating product classes and fulfilment partners
  222. =================================================
  223. Every Oscar deployment needs at least one
  224. :class:`product class <oscar.apps.catalogue.abstract_models.AbstractProductClass>`
  225. and one
  226. :class:`fulfilment partner <oscar.apps.partner.abstract_models.AbstractPartner>`.
  227. These aren't created automatically as they're highly specific to the shop you
  228. want to build.
  229. When managing your catalogue you should always use the Oscar dashboard, which
  230. provides the necessary functionality. Use your Django superuser email and password to login to:
  231. http://127.0.0.1:8000/dashboard/ and create instances of both there.
  232. It is important to note that the Django admin site is not supported. It may
  233. or may not work and is only included in the sandbox for developer's
  234. convenience.
  235. For a deployment setup, we recommend creating product classes
  236. as `data migration`_.
  237. .. _`data migration`: http://codeinthehole.com/writing/prefer-data-migrations-to-initial-data/
  238. Defining the order pipeline
  239. ===========================
  240. The order management in Oscar relies on the order pipeline that
  241. defines all the statuses an order can have and the possible transitions
  242. for any given status. Statuses in Oscar are not just used for an order
  243. but are handled on the line level as well to be able to handle partial
  244. shipping of an order.
  245. The order status pipeline is different for every shop which means that
  246. changing it is fairly straightforward in Oscar. The pipeline is defined in
  247. your ``settings.py`` file using the ``OSCAR_ORDER_STATUS_PIPELINE`` setting.
  248. You also need to specify the initial status for an order and a line item in
  249. ``OSCAR_INITIAL_ORDER_STATUS`` and ``OSCAR_INITIAL_LINE_STATUS``
  250. respectively.
  251. To give you an idea of what an order pipeline might look like take a look
  252. at the Oscar sandbox settings:
  253. .. code-block:: django
  254. OSCAR_INITIAL_ORDER_STATUS = 'Pending'
  255. OSCAR_INITIAL_LINE_STATUS = 'Pending'
  256. OSCAR_ORDER_STATUS_PIPELINE = {
  257. 'Pending': ('Being processed', 'Cancelled',),
  258. 'Being processed': ('Processed', 'Cancelled',),
  259. 'Cancelled': (),
  260. }
  261. Defining the order status pipeline is simply a dictionary of where each
  262. status is given as a key. Possible transitions into other statuses can be
  263. specified as an iterable of status names. An empty iterable defines an
  264. end point in the pipeline.
  265. With these three settings defined in your project you'll be able to see
  266. the different statuses in the order management dashboard.
  267. Next steps
  268. ==========
  269. The next step is to implement the business logic of your domain on top of
  270. Oscar. The fun part.