0
votes

I have run the python manage.py runserver command on my computer(windows 10) and it work but when I put all of codes on the google cloud platform compute engine(Ubuntu 16) and run the same command it shows all of the static files are not found. Do anyone knows how to fix it?

settings.py (ignore some irrelative parts)

import os

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

ALLOWED_HOSTS = ['*']


# Application definition

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

    'restaurants',  # 餐廳APP
    'webpack_loader',  # 整合vue和django套件
    'corsheaders'  # 處理跨域請求套件

    # 'gunicorn',  # 部署用
]

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


ROOT_URLCONF = 'foodies.urls'

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

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

 STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '../frontend/dist'),
)

WEBPACK_LOADER = {
    'DEFAULT': {
    'BUNDLE_DIR_NAME': '',
    'STATS_FILE': os.path.join(BASE_DIR, '../webpack- 
   stats.json'),
    }
}


CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
# 允許所有header請求
CORS_ALLOW_HEADERS = ('*')

static files path enter image description here

index.html enter image description here

url.py

from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
from django.views.generic import TemplateView


schema_view = get_swagger_view(title='Pastebin API')

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('restaurants.urls')),
    path('api-auth/', include('rest_framework.urls')),
    path('api-view/', schema_view),
    path('', TemplateView.as_view(
        template_name="index.html"), name='index')
]

chrome error messages enter image description here

compute engine status enter image description here

1
Did you run collectstatic?shafik
I did but still not working.Ken Hsieh
In development mode (DEBUG=True) Django automatically serves static files using runserver. In production, you'll need to configure a web server (e.g. nginx) to serve the static files for you.Will Keeling

1 Answers

0
votes

I would suggest reading this: https://docs.djangoproject.com/en/2.1/howto/static-files/deployment/

It has specific and exact instructions on the expected deployment of static files.