Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

getting_started.rst 12KB

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