Project description

The e-commerce project is similar to JINGdong Mall. The main modules include authentication, user, third-party login, home page advertisement, goods, shopping cart, order, payment and background management system. In order to improve search engine ranking, the overall page refresh is implemented by Jinja2 template engine, and the partial page refresh is implemented by vue.js.

The project operation mechanism is as follows:

Project structures,

Engineering to create

The project uses the code cloud for source code versioning. After the code cloud is created, clone it to the local directory. Then run the virtualenv venv command in the root directory of the project to create a virtual environment. Run the django-admin startproject immortal_mall command to create a Django project.

Configuring the Development Environment

The store project has two environments, the test environment and the development environment. Django projects only have one Settings configuration file after being created, but two environments require two configuration files. You need to change the way Django obtains configuration files. Create a Settings package, create dev and pro configuration files, and copy the contents of the default Settings files to dev and pro. The result is as follows

Specify the configuration files required by the development environment in the mange.py file, which will be discussed later

Configure the jiaja2 template engine

Install the jinja2 extension package PIP install jinja2 and configure it in the dev file

There is a note here, if the runtime error, warning

This is because django commented out its default template configuration. You need to remove the comment and add the new version.

Configuring the mysql Database

Creating a Database

  1. Creating a Databasecreate database meiduo charset=utf8;
  2. Create a mysql usercreate user mall identified by '123456';
  3. Authorized users can only access the immorTAL_mall databasegrant all on immortal_mall.* to 'mall'@'%';
  4. Refresh the authorizationflush privileges;

    Configuring the Database

    Database = {'default': {'ENGINE': 'django.db.backends.mysql', 'NAME': 'immortal_mall', 'HOST': '127.0.0.1', 'USER': 'zhouyajun', 'PASSWORD': '12345678', 'PORT': '3306' } }Copy the code

    Django uses the mysqlClient tool by default, which needs to be installed separatelypymysqlInstead, in the project subdirectory of the same name__init__.pyIn the file, write the following code

    import pymysql
    pymysql.install_as_MySQLdb()Copy the code

    Mysql may be prompted with an error when starting the project pair

    The File "/ Users/lixiang/env/lib/python3.6 / site - packages/django/db/backends/mysql/base. Py", line 36, In <module> raise ImproperlyConfigured(' mysqlClient 1.3.13 or newer is required; you have %s.' % Database.__version__) django.core.exceptions.ImproperlyConfigured: Newer is required; mysqlClient 1.3.13 or newer is required; You have 0.9.3.Copy the code

    Specific solutions can be referred to herezhuanlan.zhihu.com/p/76920424

Configure redis

Mall use redis as a cache service PIP install django – redis django django – redis use document here – redis – CHS. Readthedocs. IO/zh_CN/lates…

Cache configuration

CACHES = {"default": {"BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/2", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, "session": { "BACKEND": "Django_rediscache. Cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/3", "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient", } } } SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_CACHE_ALIAS = "session"Copy the code

Default is the default configuration used by Redis, using database 2, session is the configuration item used by Redis to maintain state, using database 3,

The log configuration

LOGGING = {'version': 1, 'disable_EXISTing_loGGERS ': False, # whether to disable existing logger 'formatters': 'verbose': {'format': '%(LevelName)s %(asctime)s %(module)s %(lineno)d %(message)s'}, 'simple': {'format': '%(levelName)s %(module)s %(lineno)d %(message)s'},}, 'filters': {# require_debug_true: {# django in debug mode to output log '()' : 'the django. Utils. The RequireDebugTrue',},}, 'handlers' : {#' console log processing method: {# Output logs to terminal 'level': 'INFO', 'filters': ['require_debug_true'], 'class': 'logging.streamHandler ', 'formatter': 'simple'} 'file' : {# to the log file output 'level' : 'INFO', 'class' : 'logging. Handlers. RotatingFileHandler', 'filename' : Os.path. join(os.path.dirname(BASE_DIR), 'logs/mall.log'), # log file location 'maxBytes': 300 * 1024 * 1024, 'backupCount': Handlers: {loggers': {djangos ': {# define a logger named Django. 'Handlers ': { ['console', 'file'], # propagate': True, # Propagate the log information 'level': 'INFO', # the lowest log level received by the logger},}}Copy the code

This needs to be created manually in the root directory of the projectlogsFile, when writing log, we want to be able to automatically divide the date to write, every day log to a different file, here can usepythonBuilt-in moduleTimedRotatingFileHandler, you can also customize onehandlerClass to implement. I’ve implemented my own class here calledMallRotatingFileHandlerThe effect is as follows:

This class divides log files by month, automatically names log files according to the date of the day, and defines file capacity.

Configure front-end static files

Prepare the static folder static

Specify the static file loading path

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]Copy the code

Run the project, request for a picture, http://127.0.0.1:8989/static/images/adv01.jpg, get success is configured correctly.

Display results of each catalog of the final project:

You are welcome to check out my blog, where there is more about the actual test content oh!!