Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

getting_started.rst 13KB

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