Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

getting_started.rst 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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[sorl-thumbnail]
  19. $ django-admin 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. .. tip::
  27. ``sorl-thumbnail`` is an optional dependency for image thumbnail, but is what Oscar expects
  28. to use by default. It can be replaced with ``easy-thumbnails`` or a custom thumbnail backend. If you want to
  29. use a different backend then remember to change the ``OSCAR_THUMBNAILER`` setting to point to it.
  30. .. attention::
  31. Please ensure that ``pillow``, a fork of the the Python Imaging Library
  32. (PIL), gets installed with JPEG support. Supported formats are printed
  33. when ``pillow`` is first installed.
  34. Instructions_ on how to get JPEG support are highly platform specific,
  35. but guides for ``PIL`` should work for ``pillow`` as well. Generally
  36. speaking, you need to ensure that ``libjpeg-dev`` is installed and found
  37. during installation.
  38. .. _Instructions: http://www.google.com/search?q=install+pil+with+jpeg+support
  39. Django settings
  40. ===============
  41. First, edit your settings file ``frobshop/frobshop/settings.py`` to import all of Oscar's default settings.
  42. .. code-block:: django
  43. from oscar.defaults import *
  44. Now add Oscar's context processors to the template settings, listed below:
  45. .. code-block:: django
  46. 'oscar.apps.search.context_processors.search_form',
  47. 'oscar.apps.checkout.context_processors.checkout',
  48. 'oscar.apps.communication.notifications.context_processors.notifications',
  49. 'oscar.core.context_processors.metadata',
  50. Next, modify ``INSTALLED_APPS`` to be a list, and add ``django.contrib.sites``,
  51. ``django.contrib.flatpages``, Oscar's core apps, and third-party apps that Oscar
  52. depends on. Also set ``SITE_ID``:
  53. .. code-block:: django
  54. INSTALLED_APPS = [
  55. 'django.contrib.admin',
  56. 'django.contrib.auth',
  57. 'django.contrib.contenttypes',
  58. 'django.contrib.sessions',
  59. 'django.contrib.messages',
  60. 'django.contrib.staticfiles',
  61. 'django.contrib.sites',
  62. 'django.contrib.flatpages',
  63. 'oscar.config.Shop',
  64. 'oscar.apps.analytics.apps.AnalyticsConfig',
  65. 'oscar.apps.checkout.apps.CheckoutConfig',
  66. 'oscar.apps.address.apps.AddressConfig',
  67. 'oscar.apps.shipping.apps.ShippingConfig',
  68. 'oscar.apps.catalogue.apps.CatalogueConfig',
  69. 'oscar.apps.catalogue.reviews.apps.CatalogueReviewsConfig',
  70. 'oscar.apps.communication.apps.CommunicationConfig',
  71. 'oscar.apps.partner.apps.PartnerConfig',
  72. 'oscar.apps.basket.apps.BasketConfig',
  73. 'oscar.apps.payment.apps.PaymentConfig',
  74. 'oscar.apps.offer.apps.OfferConfig',
  75. 'oscar.apps.order.apps.OrderConfig',
  76. 'oscar.apps.customer.apps.CustomerConfig',
  77. 'oscar.apps.search.apps.SearchConfig',
  78. 'oscar.apps.voucher.apps.VoucherConfig',
  79. 'oscar.apps.wishlists.apps.WishlistsConfig',
  80. 'oscar.apps.dashboard.apps.DashboardConfig',
  81. 'oscar.apps.dashboard.reports.apps.ReportsDashboardConfig',
  82. 'oscar.apps.dashboard.users.apps.UsersDashboardConfig',
  83. 'oscar.apps.dashboard.orders.apps.OrdersDashboardConfig',
  84. 'oscar.apps.dashboard.catalogue.apps.CatalogueDashboardConfig',
  85. 'oscar.apps.dashboard.offers.apps.OffersDashboardConfig',
  86. 'oscar.apps.dashboard.partners.apps.PartnersDashboardConfig',
  87. 'oscar.apps.dashboard.pages.apps.PagesDashboardConfig',
  88. 'oscar.apps.dashboard.ranges.apps.RangesDashboardConfig',
  89. 'oscar.apps.dashboard.reviews.apps.ReviewsDashboardConfig',
  90. 'oscar.apps.dashboard.vouchers.apps.VouchersDashboardConfig',
  91. 'oscar.apps.dashboard.communications.apps.CommunicationsDashboardConfig',
  92. 'oscar.apps.dashboard.shipping.apps.ShippingDashboardConfig',
  93. # 3rd-party apps that oscar depends on
  94. 'widget_tweaks',
  95. 'haystack',
  96. 'treebeard',
  97. 'sorl.thumbnail', # Default thumbnail backend, can be replaced
  98. 'django_tables2',
  99. ]
  100. SITE_ID = 1
  101. Note that Oscar requires ``django.contrib.flatpages`` which isn't
  102. included by default. ``flatpages`` also requires ``django.contrib.sites``.
  103. More info about installing ``flatpages`` is in the `Django docs`_.
  104. .. _`Django docs`: https://docs.djangoproject.com/en/stable/ref/contrib/flatpages/#installation
  105. .. tip::
  106. Oscar's default templates use django-widget-tweaks_ but it's
  107. optional really. You may decide to use your own templates that
  108. don't use either.
  109. .. _django-widget-tweaks: https://github.com/kmike/django-widget-tweaks
  110. Next, add ``oscar.apps.basket.middleware.BasketMiddleware`` and
  111. ``django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`` to
  112. your ``MIDDLEWARE`` setting.
  113. .. code-block:: django
  114. MIDDLEWARE = (
  115. ...
  116. 'oscar.apps.basket.middleware.BasketMiddleware',
  117. 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
  118. )
  119. Set your authentication backends to:
  120. .. code-block:: django
  121. AUTHENTICATION_BACKENDS = (
  122. 'oscar.apps.customer.auth_backends.EmailBackend',
  123. 'django.contrib.auth.backends.ModelBackend',
  124. )
  125. to allow customers to sign in using an email address rather than a username.
  126. Ensure that your media and static files are `configured correctly`_. This means
  127. at the least setting ``MEDIA_URL`` and ``STATIC_URL``. If you're serving files
  128. locally, you'll also need to set ``MEDIA_ROOT`` and ``STATIC_ROOT``.
  129. Check out the `sandbox settings`_ for a working example. If you're serving
  130. files from a remote storage (e.g. Amazon S3), you must manually copy a
  131. :ref:`"Image not found" image <missing-image-label>` into ``MEDIA_ROOT``.
  132. .. _`configured correctly`: https://docs.djangoproject.com/en/stable/howto/static-files/
  133. .. _sandbox settings: https://github.com/django-oscar/django-oscar/blob/master/sandbox/settings.py#L102
  134. Search backend
  135. ==============
  136. If you're happy with basic search for now, you can just add Haystack's simple
  137. backend to the ``HAYSTACK_CONNECTIONS`` option in your Django settings:
  138. .. code-block:: django
  139. HAYSTACK_CONNECTIONS = {
  140. 'default': {
  141. 'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
  142. },
  143. }
  144. Oscar uses Haystack to abstract away from different search backends.
  145. Unfortunately, writing backend-agnostic code is nonetheless hard and
  146. Apache Solr is currently the only supported production-grade backend. Your
  147. Haystack config could look something like this:
  148. .. code-block:: django
  149. HAYSTACK_CONNECTIONS = {
  150. 'default': {
  151. 'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
  152. 'URL': 'http://127.0.0.1:8983/solr',
  153. 'INCLUDE_SPELLING': True,
  154. },
  155. }
  156. Oscar includes a sample schema to get started with Solr. More information can
  157. be found in the
  158. :doc:`recipe on getting Solr up and running</howto/how_to_setup_solr>`.
  159. Database
  160. ========
  161. Check your database settings. A quick way to get started is to use SQLite:
  162. .. code-block:: django
  163. DATABASES = {
  164. 'default': {
  165. 'ENGINE': 'django.db.backends.sqlite3',
  166. 'NAME': 'db.sqlite3',
  167. 'USER': '',
  168. 'PASSWORD': '',
  169. 'HOST': '',
  170. 'PORT': '',
  171. 'ATOMIC_REQUESTS': True,
  172. }
  173. }
  174. Note that we recommend using ``ATOMIC_REQUESTS`` to tie transactions to
  175. requests.
  176. URLs
  177. ====
  178. Alter your ``frobshop/urls.py`` to include Oscar's URLs. You can also include
  179. the Django admin for debugging purposes. But please note that Oscar makes no
  180. attempts at having that be a workable interface; admin integration exists
  181. to ease the life of developers.
  182. If you have more than one language set your Django settings for ``LANGUAGES``,
  183. you will also need to include Django's i18n URLs:
  184. .. code-block:: django
  185. from django.apps import apps
  186. from django.urls import include, path
  187. from django.contrib import admin
  188. urlpatterns = [
  189. path('i18n/', include('django.conf.urls.i18n')),
  190. # The Django admin is not officially supported; expect breakage.
  191. # Nonetheless, it's often useful for debugging.
  192. path('admin/', admin.site.urls),
  193. path('', include(apps.get_app_config('oscar').urls[0])),
  194. ]
  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.