django oscar simple tutorial

Since it became necessary to create an ec site by myself, if you make full use of the package and framework, the EC site will be completed (should). So I decided to use Django for the web framework and Oscar for the EC package. It's good to decide, but there are few Japanese materials. The official English documentation also says "For more information, see Sandbox and github. ) De ”is not kind and difficult to understand.

This article shows the procedure to build an EC site ~~ and feel like ~~. Basically, I'm just tracing django-oscar / Building your own shop of django-oscar, but for reference.

Finally, we will build an EC site without a payment function. Also, this is just a tutorial.

Development environment

Setup

In Documentation, I use mkvirtualenv or virtualenv, but this time I entered it on my computer from the beginning. I will utilize Anaconda that I had.

Build a virtual environment with Anaconda. First, build a python 3.8 environment.

$ conda create -n py38 python=3.8

Enable virtual environment.

$ conda info -e #Environmental confirmation
$ conda activate py38

Install django-oscar with pip.

$ pip install django-oscar

Some packages are missing, so install them as well.

$ pip install sorl-thumbnail
$ pip install pysolr

As of January 2020, the version is as follows

Create a Django project

$ django-admin startproject frobshop

Django settings (settings.py)

Add the following to the beginning of the line frobshop.frobshop.settings.py. Oscar's default settings are imported.

python:frobshop.frobshop.settings.py


from oscar.defaults import *

Added Oscar context processors to the template.

python:frobshop.frobshop.settings.py


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.core.context_processors.metadata',
            ],
        },
    },
]

Rewrite ʻINSTALLED_APPS as follows and setSITE_ID`.

python:frobshop.frobshop.settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django.contrib.sites',
    'django.contrib.flatpages',

    'oscar',
    'oscar.apps.analytics',
    'oscar.apps.checkout',
    'oscar.apps.address',
    'oscar.apps.shipping',
    'oscar.apps.catalogue',
    'oscar.apps.catalogue.reviews',
    'oscar.apps.partner',
    'oscar.apps.basket',
    'oscar.apps.payment',
    'oscar.apps.offer',
    'oscar.apps.order',
    'oscar.apps.customer',
    'oscar.apps.search',
    'oscar.apps.voucher',
    'oscar.apps.wishlists',
    'oscar.apps.dashboard',
    'oscar.apps.dashboard.reports',
    'oscar.apps.dashboard.users',
    'oscar.apps.dashboard.orders',
    'oscar.apps.dashboard.catalogue',
    'oscar.apps.dashboard.offers',
    'oscar.apps.dashboard.partners',
    'oscar.apps.dashboard.pages',
    'oscar.apps.dashboard.ranges',
    'oscar.apps.dashboard.reviews',
    'oscar.apps.dashboard.vouchers',
    'oscar.apps.dashboard.communications',
    'oscar.apps.dashboard.shipping',

    # 3rd-party apps that oscar depends on
    'widget_tweaks',
    'haystack',
    'treebeard',
    'sorl.thumbnail',
    'django_tables2',
]

SITE_ID = 1

Added ʻoscar.apps.basket.middleware.BasketMiddlewareanddjango.contrib.flatpages.middleware.FlatpageFallbackMiddleware to the MIDDLEWARE` settings.

python:frobshop.frobshop.settings.py


MIDDLEWARE = (
    ...
    'oscar.apps.basket.middleware.BasketMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

Added ʻAUTHENTICATION_BACKENDS` setting so that you can authenticate with your email address.

python:frobshop.frobshop.settings.py


AUTHENTICATION_BACKENDS = (
    'oscar.apps.customer.auth_backends.EmailBackend',
    'django.contrib.auth.backends.ModelBackend',
)

Japan support

To support Japan, rewrite the language code and time zone as follows.

python:frobshop.frobshop.settings.py


LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'

Set currency and currency format (because oscar's default format displays up to two decimal places)

python:frobshop.frobshop.settings.py


OSCAR_DEFAULT_CURRENCY = 'JPY'
OSCAR_CURRENCY_FORMAT = '¤#,##0'

URLs

Rewrite frobshop.frobshop.urls.py as follows.

python:frobshop.frobshop.urls.py


from django.apps import apps
from django.urls import include, path  # > Django-2.0
from django.contrib import admin

urlpatterns = [
    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.

    path('admin/', admin.site.urls),  # > Django-2.0

    path('', include(apps.get_app_config('oscar').urls[0])),  # > Django-2.0
]

Search backend

Set up the search system. If you want to try it for the time being, make the following settings.

python:frobshop.frobshop.settings.py


HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
    },
}

ʻWhen setting Apache Solr, set up [Apache Solr](https://django-oscar.readthedocs.io/en/stable/howto/how_to_setup_solr.html) in advance, and then do the following in frobshop.frobshop Added to .settings.py`.

python:frobshop.frobshop.settings.py


HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://127.0.0.1:8983/solr',
        'INCLUDE_SPELLING': True,
    },
}

Database

If you want to check the operation immediately, use sqlite for the database. Edit frobshop.frobshop.settings.py and rewrite the DATABASES settings as follows.

python:frobshop.frobshop.settings.py


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
        'ATOMIC_REQUESTS': True,
    }
}

Confirmation of site startup

Perform migration.

$ python manage.py migrate

Start the server and check if the site can be started.

$ python manage.py runserver

Initial data

Up to this point, an EC site with no products or registered users will be built. Make various settings to make it look like an EC site.

Country model

Put the initial data of 249 countries in the country model (address_country).

$ pip install pycountry
$ python manage.py oscar_populate_countries

There are too many 249 countries, so only Japan will be registered. First, after connecting to the DB, set is_shipping_country to 1 for Japan only and 0 for the others. Country name selection does not appear when registering an address (Japan becomes the default)

$ python manage.py dbshell
SQLite version 3.30.1 2019-10-10 20:19:45
Enter ".help" for usage hints.
sqlite> UPDATE address_country SET is_shipping_country = 0 WHERE printable_name != 'Japan';

User data

Input data referring to Inject initial data for model.

First, input user data. Enter User information registered in Sandbox.

$ python manage.py loaddata frobshop/fixtures/auth.json
Installed 2 object(s) from 1 fixture(s)

The administrator user has the following settings.

username: superuser
email: [email protected]
password: testing

The staff account is as follows.

username: staff
email: [email protected]
password: testing

Product data

Input product data. There are two types of product data on the Official Sandbox site. multi-stockrecord-product.json and child_products.json //github.com/django-oscar/django-oscar/blob/master/sandbox/fixtures/child_products.json).

The differences between them will be explained. child_products.json is a different type of product (for example, a T-shirt) It shows how to set when you want to correspond to (size, color, etc.). For example, for T-shirts, set ʻAttributes to sizes L, M, S`.

multi-stockrecord-product.json runs an ec site In that case, there may be multiple warehouses or suppliers that manage inventory. Multiple suppliers are managed by partner.stockrecord in oscar.

If you want to input product data for the time being, input one of the jsons to the database.

$ python manage.py loaddata frobshop/fixtures/multi-stockrecord-product.json

Below is a link to a document that shows the idea of how oscar manages products.

Order pipeline

When an order comes in via the ec site, set how to process the order in the order pipeline. For example, add the following to settings.py. In the example below, Pending is the first status, and the order is PendingBeing processed Processed. It is assumed that if canceled in the middle, the status will be Cancelled.

python:frobshop.frobshop.settings.py


OSCAR_INITIAL_ORDER_STATUS = 'Pending'
OSCAR_INITIAL_LINE_STATUS = 'Pending'
OSCAR_ORDER_STATUS_PIPELINE = {
    'Pending': ('Being processed', 'Cancelled',),
    'Being processed': ('Processed', 'Cancelled',),
    'Cancelled': (),
}

Email settings

If the above settings are left as they are, a ConnectionRefusedError at / checkout / preview / will appear when the order is confirmed (when you press Place order on the Preview order screen).

[Building a full ecommerce site part 5: testing directing customers to Oscar's thank-you page after payment](https://notathoughtexperiment.me/blog/building-a-full-ecommerce-site-part-5-send-clients- The reason is written in to-the-thank-you-page-after-payment /), but I couldn't understand it even after reading it.

As a workaround, add ʻEMAIL_BACKEND to settings.py` as follows.

python:frobshop.frobshop.settings.py


EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Summary

For the time being, if you set up so far, all the functions of the EC site will be covered (other than the payment function).

To build an EC site (for example, add an image to a product) Sandbox site settings on Github Please try to build it with reference to.

If you feel like it, I would like to write an implementation of the payment function.

Reference site

I referred to the following site.

Recommended Posts

django oscar simple tutorial
Python Django Tutorial (5)
Python Django Tutorial (2)
django tutorial memo
Python Django Tutorial (8)
Python Django Tutorial (6)
Start Django Tutorial 1
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
Python Django tutorial summary
Django Polymorphic Associations Tutorial
Django Girls Tutorial Note
Django beginners create simple apps 3
Django beginners create simple apps 1
Get started with Django! ~ Tutorial ④ ~
Get started with Django! ~ Tutorial ⑥ ~
Django beginners create simple apps 2
Django beginners make simple apps 4
Python Django Tutorial Cheat Sheet
Django beginners create simple apps 5
Django
Django Girls Tutorial Summary First Half
Stumble when doing the django 1.7 tutorial
Deploy the Django tutorial to IIS ①
A simple RSS reader made with Django
Django tutorial summary for beginners by beginners ③ (View)
Django Tutorial (Blog App Creation) ⑤ --Article Creation Function
Django Foreign Key Tutorial Ends in 10 Minutes
Django Oscar (EC Package) Japan Usage Guide
Django Tutorial (Blog App Creation) ④ --Unit Test
Django tutorial summary for beginners by beginners ⑤ (test)