antoinemartin / django-windows-tools

Django application providing management commands to host Django projects in Windows environments
BSD 2-Clause "Simplified" License
51 stars 13 forks source link

Django Project is not starting #32

Open udayapraveen opened 4 years ago

udayapraveen commented 4 years ago

Hi Martin,

When I create a new project and follow the steps that you have described your document to start the Django project as the windows process is working fine. But the same when I apply to my project that has MongoDB connectivity in the settings file it fails to start giving the following error log.

When ran with net start service name

[INFO/Process-1] Starting command : C:\Python\Python37\lib\site-packages\django_windows_tools\service.py runserver --noreload --insecure 0.0.0.0:8000
[INFO/Process-1] process shutting down
[INFO/Process-1] process exiting with exitcode 0

When ran with python service.py debug

[INFO/Process-1] Starting command : C:\Python\Python37\lib\site-packages\django_windows_tools\service.py runserver --noreload --insecure 0.0.0.0:8000
[INFO/Process-1] [19/Feb/2020 11:59:04] "GET / HTTP/1.1" 200 750
[INFO/Process-1] [19/Feb/2020 11:59:04] "GET /static/assets/2.jpg HTTP/1.1" 200 32374
[INFO/Process-1] [19/Feb/2020 11:59:04] "GET /static/assets/logo.ico HTTP/1.1" 200 4725

I have done the service.py install/update but still, the issue is same

settings.py file:

"""
Django settings for DS_CENTRAL project.

Generated by 'django-admin startproject' using Django 2.2.4.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os
from pathlib import Path
from pymongo import MongoClient
from socket import gethostname, gethostbyname

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxx'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']

APPEND_SLASH=False

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'django_windows_tools',
]
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = ()
CORS_ORIGIN_REGEX_WHITELIST = ()
CORS_ALLOW_HEADERS = ('x-requested-with', 'content-type', 'accept', 'origin', 'authorization', 'x-csrf-token', 'token',)

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware',

]

ROOT_URLCONF = 'DS_CENTRAL.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'DS_CENTRAL.wsgi.application'

# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
client = MongoClient('localhost', 27017)
DATABASE = client.DS_CENTRAL_Q

# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )

STATIC_URL = '/static/'
MEDIA_URL = ''

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
print (STATIC_ROOT)
Path(STATIC_ROOT).mkdir(parents=True, exist_ok=True)

MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'static')
Path(MEDIA_ROOT).mkdir(parents=True, exist_ok=True)
print (MEDIA_ROOT)
EXCEL_UPLOADS =  os.path.join(BASE_DIR,'static', 'static','uploads')
Path(EXCEL_UPLOADS).mkdir(parents=True, exist_ok=True)

LogFile_PATH =  os.path.join(BASE_DIR,'static', 'static','logs')
Path(LogFile_PATH).mkdir(parents=True, exist_ok=True)

# setting the maximum request data size to 10 MB
DATA_UPLOAD_MAX_MEMORY_SIZE = 1000000000

ACTION_MAIL = []

service.py

#!/usr/bin/env python

import os
import os.path
import sys
import win32serviceutil

# This is my base path
base_path = os.path.dirname(os.path.abspath(__file__))
print ('base_path = ', base_path)
if not base_path in sys.path:
    sys.path.append(base_path)
    print (sys.path.append(base_path))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DS_CENTRAL.settings")

from django_windows_tools.service import DjangoService,test_commands

class Service(DjangoService):

    _base_path = base_path
    _svc_name_ = "django-DsCentral-service"
    _svc_display_name_ = "Django DS_CENTRAL background service"
    _config_filename = "service.ini"

if __name__ == "__main__":    
    if len(sys.argv) > 1 and sys.argv[1] == 'test':
        test_commands(base_path)
    else:
        win32serviceutil.HandleCommandLine(Service)

service.ini

[services]
# Services to be run on all machines
run=runserver
clean=C:\Users\LineProject\Desktop\static\static\logs\service.log

# [BEATSERVER]
# There should be only one machine with the celerybeat service
# run=celeryd celerybeat
# clean=d:\logs\celerybeat.pid;d:\logs\beat.log;d:\logs\celery.log

# [celeryd]
# command=celeryd
# parameters=-f d:\logs\celery.log -l info

# [celerybeat]
# command=celerybeat
# parameters=-f d:\logs\beat.log -l info --pidfile=d:\logs\celerybeat.pid

[runserver]
# Runs the debug server and listen on port 8000
# This one is just an example to show that any manage command can be used
command=runserver
parameters=--noreload --insecure 0.0.0.0:8000

[log]
filename=C:\Users\LineProject\Desktop\static\static\logs\service.log
level=DEBUG

service.log

[INFO/Process-1] Starting command : C:\Python\Python37\lib\site-packages\django_windows_tools\service.py runserver --noreload --insecure 0.0.0.0:8000
[INFO/Process-1] process shutting down
[DEBUG/Process-1] running all "atexit" finalizers with priority >= 0
[DEBUG/Process-1] running the remaining "atexit" finalizers
[INFO/Process-1] process exiting with exitcode 0

Thanks & regards, Mayur Kumar Sharma

mrbean-bremen commented 4 years ago

Disclaimer: this project is not actively maintained for several years now, and the last Django version that has been tested was 1.11.

Anyway, I understand that you got the service running with the default settings and the SQLite database. However, I don't know what these lines do or are supposed to do:

client = MongoClient('localhost', 27017)
DATABASE = client.DS_CENTRAL_Q

DATABASE is not used by Django, as far as I know, so that must be something you do to connect to MongoDB while still using SQLite as the DB. The rest of the configuration seems to be the default, so I guess something related to MongoDB is causing the problem. Not knowing anything about MongoDB, I would guess that the integration with Django is not working as intended. Maybe you need something like djongo?

(I edited the formatting of your question to be more readable and removed the secrete key)