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

getting_started.rst 12KB

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