Django static files can't be loaded on the google compute engine












0















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










share|improve this question























  • Did you run collectstatic?

    – Shafikur Rahman
    Jan 20 at 12:21











  • I did but still not working.

    – Ken Hsieh
    Jan 20 at 13:31











  • 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
    Jan 20 at 14:58


















0















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










share|improve this question























  • Did you run collectstatic?

    – Shafikur Rahman
    Jan 20 at 12:21











  • I did but still not working.

    – Ken Hsieh
    Jan 20 at 13:31











  • 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
    Jan 20 at 14:58
















0












0








0








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










share|improve this question














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







python django google-cloud-platform google-compute-engine django-staticfiles






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 12:20









Ken HsiehKen Hsieh

105




105













  • Did you run collectstatic?

    – Shafikur Rahman
    Jan 20 at 12:21











  • I did but still not working.

    – Ken Hsieh
    Jan 20 at 13:31











  • 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
    Jan 20 at 14:58





















  • Did you run collectstatic?

    – Shafikur Rahman
    Jan 20 at 12:21











  • I did but still not working.

    – Ken Hsieh
    Jan 20 at 13:31











  • 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
    Jan 20 at 14:58



















Did you run collectstatic?

– Shafikur Rahman
Jan 20 at 12:21





Did you run collectstatic?

– Shafikur Rahman
Jan 20 at 12:21













I did but still not working.

– Ken Hsieh
Jan 20 at 13:31





I did but still not working.

– Ken Hsieh
Jan 20 at 13:31













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
Jan 20 at 14:58







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
Jan 20 at 14:58














1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer
























  • I have run python mange.py collectstatic but it still not working. I will try to set nginx to serve satic files.

    – Ken Hsieh
    Jan 20 at 13:41











  • But It's still confused that it could run in local instead of on the cloud.

    – Ken Hsieh
    Jan 20 at 13:55











  • If you're into reading the Django code base, it might help you understand. github.com/django/django Either way, a deployed project can run as it did when you were doing local development if you want, but its not recommended.

    – Jaberwocky
    Jan 22 at 6:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276365%2fdjango-static-files-cant-be-loaded-on-the-google-compute-engine%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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.






share|improve this answer
























  • I have run python mange.py collectstatic but it still not working. I will try to set nginx to serve satic files.

    – Ken Hsieh
    Jan 20 at 13:41











  • But It's still confused that it could run in local instead of on the cloud.

    – Ken Hsieh
    Jan 20 at 13:55











  • If you're into reading the Django code base, it might help you understand. github.com/django/django Either way, a deployed project can run as it did when you were doing local development if you want, but its not recommended.

    – Jaberwocky
    Jan 22 at 6:19
















0














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.






share|improve this answer
























  • I have run python mange.py collectstatic but it still not working. I will try to set nginx to serve satic files.

    – Ken Hsieh
    Jan 20 at 13:41











  • But It's still confused that it could run in local instead of on the cloud.

    – Ken Hsieh
    Jan 20 at 13:55











  • If you're into reading the Django code base, it might help you understand. github.com/django/django Either way, a deployed project can run as it did when you were doing local development if you want, but its not recommended.

    – Jaberwocky
    Jan 22 at 6:19














0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 20 at 13:14









JaberwockyJaberwocky

6521316




6521316













  • I have run python mange.py collectstatic but it still not working. I will try to set nginx to serve satic files.

    – Ken Hsieh
    Jan 20 at 13:41











  • But It's still confused that it could run in local instead of on the cloud.

    – Ken Hsieh
    Jan 20 at 13:55











  • If you're into reading the Django code base, it might help you understand. github.com/django/django Either way, a deployed project can run as it did when you were doing local development if you want, but its not recommended.

    – Jaberwocky
    Jan 22 at 6:19



















  • I have run python mange.py collectstatic but it still not working. I will try to set nginx to serve satic files.

    – Ken Hsieh
    Jan 20 at 13:41











  • But It's still confused that it could run in local instead of on the cloud.

    – Ken Hsieh
    Jan 20 at 13:55











  • If you're into reading the Django code base, it might help you understand. github.com/django/django Either way, a deployed project can run as it did when you were doing local development if you want, but its not recommended.

    – Jaberwocky
    Jan 22 at 6:19

















I have run python mange.py collectstatic but it still not working. I will try to set nginx to serve satic files.

– Ken Hsieh
Jan 20 at 13:41





I have run python mange.py collectstatic but it still not working. I will try to set nginx to serve satic files.

– Ken Hsieh
Jan 20 at 13:41













But It's still confused that it could run in local instead of on the cloud.

– Ken Hsieh
Jan 20 at 13:55





But It's still confused that it could run in local instead of on the cloud.

– Ken Hsieh
Jan 20 at 13:55













If you're into reading the Django code base, it might help you understand. github.com/django/django Either way, a deployed project can run as it did when you were doing local development if you want, but its not recommended.

– Jaberwocky
Jan 22 at 6:19





If you're into reading the Django code base, it might help you understand. github.com/django/django Either way, a deployed project can run as it did when you were doing local development if you want, but its not recommended.

– Jaberwocky
Jan 22 at 6:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276365%2fdjango-static-files-cant-be-loaded-on-the-google-compute-engine%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage