建立自己的商店 - cucy/djoscar GitHub Wiki
为了简单起见,假设您正在从头开始构建一个新的电子商务项目,并决定使用奥斯卡(Oscar)。我们把这个商店叫做“泡沫店(frobshop)”
Oscar
and itsdependencies
Install$ mkvirtualenv oscar
$ pip install django-oscar
$ django-admin.py startproject frobshop
Django settings
First, edit your settings file frobshop.frobshop.settings.py
to import all of Oscar’s default settings.
from oscar.defaults import *
现在修改你的“模板(TEMPLATES
)”,以包含主奥斯卡模板目录,并添加额外的上下文处理器。
from oscar import OSCAR_MAIN_TEMPLATE_DIR
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
OSCAR_MAIN_TEMPLATE_DIR
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',
],
},
},
]
Next, modify INSTALLED_APPS
to be a list, add django.contrib.sites
, django.contrib.flatpages
, and widget_tweaks
and append Oscar’s core apps. Also set SITE_ID
:
from oscar import get_core_apps
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',
...
'compressor',
'widget_tweaks',
] + get_core_apps()
SITE_ID = 1
Note that Oscar requires django.contrib.flatpages
which isn’t included by default. flatpages
also requires django.contrib.sites
. More info about installing flatpages
is in the Django docs.
Next, add oscar.apps.basket.middleware.BasketMiddleware and django.contrib.flatpages.middleware.FlatpageFallbackMiddleware to your MIDDLEWARE setting.
MIDDLEWARE = (
...
'oscar.apps.basket.middleware.BasketMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
Set your auth backends
to:
AUTHENTICATION_BACKENDS = (
'oscar.apps.customer.auth_backends.EmailBackend',
'django.contrib.auth.backends.ModelBackend',
)
to allow customers to sign in using an email address rather than a username.
Ensure that your media and static files are configured correctly. This means at the least setting MEDIA_URL
and STATIC_URL
. If you’re serving files locally, you’ll also need to set MEDIA_ROOT
and STATIC_ROOT
. Check out the sandbox settings for a working example. If you’re serving files from a remote storage (e.g. Amazon S3), you must manually copy a “Image not found” image into MEDIA_ROOT
.
urls.py
from django.conf.urls import include, url # < Django-2.0
# from django.urls import include, path # > Django-2.0
from django.contrib import admin
from oscar.app import application
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
# path('i18n/', include('django.conf.urls.i18n')), # > Django-2.0
# The Django admin is not officially supported; expect breakage.
# Nonetheless, it's often useful for debugging.
url(r'^admin/', admin.site.urls),
# path('admin/', admin.site.urls), # > Django-2.0
url(r'', application.urls),
# path('', application.urls), # > Django-2.0
]
Search backend
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
}
奥斯卡使用Haystack来抽象出不同的搜索后端。不幸的是,编写后端不可知代码仍然很难,而Apache SoR是目前唯一支持的产品级后端。你的Haystack配置可以看起来像这样:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
'URL': 'http://127.0.0.1:8983/solr',
'INCLUDE_SPELLING': True,
},
}
Oscar includes a sample schema to get started with Solr. More information can be found in therecipe on getting Solr up and running.
Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite3',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
'ATOMIC_REQUESTS': True,
}
}
Create database
$ python manage.py migrate
$ python manage.py runserver
Initial data
缺省签出过程需要有一个国家的装运地址。奥斯卡使用具有标志的国家的模型来指示哪些是有效的运输国,因此必须在客户可以签出之前填写“国家”数据库表。
The easiest way to achieve this is to use country data from the pycountry package. Oscar ships with a management command to parse that data: 实现这一点的最简单方法是使用[PyWrand ](HTTPS://PyP.PythOn.Org/PyPI/PyFrand)包中的国家数据。奥斯卡用一个管理命令来分析数据:
$ pip install pycountry
[...]
$ python manage.py oscar_populate_countries
By default, this command will mark all countries as a shipping country. Call it with the --no-shipping
option to prevent that. You then need to manually mark at least one country as a shipping country.
默认情况下,这个命令将标记所有国家作为航运国。用“不送货”选项来调用,以防止这种情况发生。然后,您需要手动将至少一个国家标记为航运国。
Creating product classes and fulfillment partners 创建产品类和实现伙伴
Every Oscar deployment needs at least one product class
and one fulfillment partner
. These aren’t created automatically as they’re highly specific to the shop you want to build.
When managing your catalogue you should always use the Oscar dashboard, which provides the necessary functionality. Use your Django superuser email and password to login to:http://127.0.0.1:8000/dashboard/ and create instances of both there.
每一个奥斯卡部署需要至少一个产品类和一个履行伙伴。它们不是自动创建的,因为它们对您要构建的商店非常特定。 在管理目录时,应始终使用奥斯卡仪表板,它提供必要的功能。使用您的Django超级用户电子邮件和密码登录:http://127.0.0.1:8000 /仪表板/并创建两者的实例。
值得注意的是,不支持Django管理站点。它可能或可能不工作,只包含在沙箱中,便于开发人员使用。
对于部署设置,建议将产品类创建为数据迁移。
Defining the order pipeline 定义订单管道
The order management in Oscar relies on the order pipeline that defines all the statuses an order can have and the possible transitions for any given status. Statuses in Oscar are not just used for an order but are handled on the line level as well to be able to handle partial shipping of an order.
奥斯卡中的订单管理依赖于定义一个订单所能拥有的所有状态的订单流水线和任何给定状态的可能转换。奥斯卡中的状态不只是用于订单,而是在线路级别上处理,以便能够处理订单的部分装运。
The order status pipeline is different for every shop which means that changing it is fairly straightforward in Oscar. The pipeline is defined in your settings.py
file using the OSCAR_ORDER_STATUS_PIPELINE
setting. You also need to specify the initial status for an order and a line item in OSCAR_INITIAL_ORDER_STATUS
and OSCAR_INITIAL_LINE_STATUS
respectively.
奥斯卡中的订单管理依赖于定义一个订单所能拥有的所有状态的订单流水线和任何给定状态的可能转换。奥斯卡中的状态不只是用于订单,而是在线路级别上处理,以便能够处理订单的部分装运。
The order status pipeline is different for every shop which means that changing it is fairly straightforward in Oscar. The pipeline is defined in your settings.py
file using the OSCAR_ORDER_STATUS_PIPELINE
setting. You also need to specify the initial status for an order and a line item in OSCAR_INITIAL_ORDER_STATUS
and OSCAR_INITIAL_LINE_STATUS
respectively.
订单状态流水线对于每个商店都是不同的,这意味着在奥斯卡中改变它是相当简单的。管道使用'OsCalOrthOrthyStasuxPixPix'设置在您的Stase.Py
文件中定义。还需要在“OsCARPrimalAlgOrthOrthPosith'状态”和“OsCARPrimalLyLyLySturialStase'”中指定一个订单和一个行项目的初始状态。
为了让你知道订单流水线的样子,看看奥斯卡沙箱设置:
OSCAR_INITIAL_ORDER_STATUS = 'Pending'
OSCAR_INITIAL_LINE_STATUS = 'Pending'
OSCAR_ORDER_STATUS_PIPELINE = {
'Pending': ('Being processed', 'Cancelled',),
'Being processed': ('Processed', 'Cancelled',),
'Cancelled': (),
}
Defining the order status pipeline is simply a dictionary of where each status is given as a key. Possible transitions into other statuses can be specified as an iterable of status names. An empty iterable defines an end point in the pipeline.
With these three settings defined in your project you’ll be able to see the different statuses in the order management dashboard.
定义订单状态管道只是一个字典,其中每个状态都作为一个键给出。可以将其他状态的可能转换指定为状态名的迭代。空迭代可定义管道中的端点。 通过在项目中定义的这三个设置,您将能够看到订单管理仪表板中的不同状态。
Next steps
The next step is to implement the business logic of your domain on top of Oscar. The fun part.
The next step is to implement the business logic of your domain on top of Oscar. The fun part.