Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

getting_started.rst 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. ============================
  2. Start 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 `Sandbox site`_ in case you have
  8. trouble with the below instructions.
  9. .. _`Sandbox site`: https://github.com/tangentlabs/django-oscar/tree/master/sites/sandbox
  10. Install by hand
  11. ===============
  12. Install Oscar (which will install Django as a dependency), then create the
  13. project:
  14. .. code-block:: bash
  15. pip install django-oscar
  16. django-admin.py startproject frobshop
  17. This will create a folder ``frobshop`` for your project.
  18. Settings
  19. --------
  20. Now edit your settings file ``frobshop.frobshop.settings.py`` to specify a
  21. database (we use SQLite for simplicity):
  22. .. code-block:: django
  23. DATABASES = {
  24. 'default': {
  25. 'ENGINE': 'django.db.backends.sqlite3',
  26. 'NAME': 'db.sqlite3',
  27. 'USER': '',
  28. 'PASSWORD': '',
  29. 'HOST': '',
  30. 'PORT': '',
  31. }
  32. }
  33. Then, add ``oscar.apps.basket.middleware.BasketMiddleware`` to
  34. ``MIDDLEWARE_CLASSES``. It is also recommended to use
  35. ``django.middleware.transaction.TransactionMiddleware`` too
  36. Now set ``TEMPLATE_CONTEXT_PROCESSORS`` to:
  37. .. code-block:: django
  38. TEMPLATE_CONTEXT_PROCESSORS = (
  39. "django.contrib.auth.context_processors.auth",
  40. "django.core.context_processors.request",
  41. "django.core.context_processors.debug",
  42. "django.core.context_processors.i18n",
  43. "django.core.context_processors.media",
  44. "django.core.context_processors.static",
  45. "django.contrib.messages.context_processors.messages",
  46. 'oscar.apps.search.context_processors.search_form',
  47. 'oscar.apps.promotions.context_processors.promotions',
  48. 'oscar.apps.checkout.context_processors.checkout',
  49. 'oscar.apps.customer.notifications.context_processors.notifications',
  50. 'oscar.core.context_processors.metadata',
  51. )
  52. Next, modify ``INSTALLED_APPS`` to be a list, add ``South`` and ``compressor``
  53. and append Oscar's core apps:
  54. .. code-block:: django
  55. from oscar import get_core_apps
  56. INSTALLED_APPS = [
  57. 'django.contrib.auth',
  58. 'django.contrib.contenttypes',
  59. 'django.contrib.sessions',
  60. 'django.contrib.sites',
  61. 'django.contrib.messages',
  62. 'django.contrib.flatpages',
  63. ...
  64. 'south',
  65. 'compressor',
  66. ] + get_core_apps()
  67. Note that Oscar requires ``django.contrib.messages`` and
  68. ``django.contrib.flatpages`` which aren't included by default.
  69. Next, add ``django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`` to
  70. your ``MIDDLEWARE_CLASSES`` setting:
  71. .. code-block:: django
  72. MIDDLEWARE_CLASSES = (
  73. ...
  74. 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
  75. )
  76. More info about `django-flatpages installation`_ at the django-project website.
  77. .. _`django-flatpages installation`: https://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#installation
  78. .. tip::
  79. Oscar's default templates use django-compressor_ but it's optional really.
  80. You may decide to use your own templates that don't use compressor. Hence
  81. why it is not one of the 'core apps'.
  82. .. _django-compressor: https://github.com/jezdez/django_compressor
  83. Now set your auth backends to:
  84. .. code-block:: django
  85. AUTHENTICATION_BACKENDS = (
  86. 'oscar.apps.customer.auth_backends.Emailbackend',
  87. 'django.contrib.auth.backends.ModelBackend',
  88. )
  89. to allow customers to sign in using an email address rather than a username.
  90. Modify your ``TEMPLATE_DIRS`` to include the main Oscar template directory:
  91. .. code-block:: django
  92. from oscar import OSCAR_MAIN_TEMPLATE_DIR
  93. TEMPLATE_DIRS = TEMPLATE_DIRS + (OSCAR_MAIN_TEMPLATE_DIR,)
  94. Oscar currently uses Haystack for search so you need to specify:
  95. .. code-block:: django
  96. HAYSTACK_CONNECTIONS = {
  97. 'default': {
  98. 'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
  99. },
  100. }
  101. When moving towards production, you'll obviously need to switch to a real search
  102. backend.
  103. The last addition to the settings file is to import all of Oscar's default settings:
  104. .. code-block:: django
  105. from oscar.defaults import *
  106. URLs
  107. ----
  108. Alter your ``frobshop/urls.py`` to include Oscar's URLs:
  109. .. code-block:: django
  110. from django.conf.urls import patterns, include, url
  111. from oscar.app import application
  112. urlpatterns = patterns('',
  113. (r'', include(application.urls))
  114. )
  115. Database
  116. --------
  117. Then create the database and the shop should be browsable:
  118. .. code-block:: bash
  119. python manage.py syncdb --noinput
  120. python manage.py migrate
  121. You should now have a running Oscar install that you can browse.
  122. Defining the order pipeline
  123. ---------------------------
  124. The order management in Oscar relies on the order pipeline that
  125. defines all the statuses an order can have and the possible transitions
  126. for any given status. Statuses in Oscar are not just used for an order
  127. but are handled on the line level as well to be able to handle partial
  128. shipping of an order.
  129. The order status pipeline is different for every shop which means that
  130. changing it is fairly straightforward in Oscar. The pipeline is defined in
  131. your ``settings.py`` file using the ``OSCAR_ORDER_STATUS_PIPELINE`` setting.
  132. You also need to specify the initial status for an order and a line item in
  133. ``OSCAR_INITIAL_ORDER_STATUS`` and ``OSCAR_INITIAL_LINE_STATUS``
  134. respectively.
  135. To give you an idea of what an order pipeline might look like take a look
  136. at the Oscar sandbox settings:
  137. .. code-block:: django
  138. OSCAR_INITIAL_ORDER_STATUS = 'Pending'
  139. OSCAR_INITIAL_LINE_STATUS = 'Pending'
  140. OSCAR_ORDER_STATUS_PIPELINE = {
  141. 'Pending': ('Being processed', 'Cancelled',),
  142. 'Being processed': ('Processed', 'Cancelled',),
  143. 'Cancelled': (),
  144. }
  145. Defining the order status pipeline is simply a dictionary of where each
  146. status is given as a key. Possible transitions into other statuses can be
  147. specified as an iterable of status names. An empty iterable defines an
  148. end point in the pipeline.
  149. With these three settings defined in your project you'll be able to see
  150. the different statuses in the order management dashboard.
  151. Install using Tangent's boilerplate Django project
  152. ==================================================
  153. The easiest way to get started is to use Tangent's `template Django project`_
  154. although it is tailored to an agency structure which may not suit everyone.
  155. .. _`template Django project`: https://github.com/tangentlabs/tangent-django-boilerplate
  156. Set up a virtualenv_, and create a new project using the ``startproject``
  157. management command:
  158. .. code-block:: bash
  159. mkvirtualenv frobshop # using virtualenvwrapper
  160. pip install Django
  161. django-admin.py startproject frobshop \
  162. --template=https://github.com/tangentlabs/tangent-django-boilerplate/zipball/master
  163. .. _virtualenv: http://www.virtualenv.org/en/latest/
  164. This will create a folder ``frobshop`` which is an entire templated project that
  165. follows Tangent's conventions. The structure is::
  166. frobshop/
  167. docs/
  168. www/
  169. conf/
  170. deploy/
  171. public/
  172. static/
  173. templates/
  174. manage.py
  175. settings.py
  176. settings_test.py
  177. urls.py
  178. urls_oscar.py
  179. README.rst
  180. fabconfig.py
  181. fabfile.py
  182. deploy-to-test.sh
  183. deploy-to-stage.sh
  184. deploy-to-prod.sh
  185. Replace a few files with Oscar-specific versions (the templated project can be
  186. used for non-Oscar projects too):
  187. .. code-block:: bash
  188. mv frobshop/www/urls{_oscar,}.py
  189. mv frobshop/www/deploy/requirements{_oscar,}.txt
  190. mv frobshop/www/conf/default{_oscar,}.py
  191. Install dependencies:
  192. .. code-block:: bash
  193. cd frobshop/www
  194. pip install -r deploy/requirements.txt
  195. Create database:
  196. .. code-block:: bash
  197. python manage.py syncdb --noinput
  198. python manage.py migrate
  199. And that should be it.
  200. Next steps
  201. ==========
  202. The next step is to implement the business logic of your domain on top of
  203. Oscar. The fun part.