I am using a Django version - 2.0.6.
and running the server on google compute engine VM instance.
My apache files are not configured to server the production base and local settings differently.
Currently the settings are running from base.py and local.py.
I have configured my media and static files like this:
my settings module(both base.py and local.py):
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), "static-root")
MEDIA_URL = '/media/'
MEDIA_DIR = os.path.join(BASE_DIR,'media')
MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), "media-root")
my urls.py:
urlpatterns = [
....
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
When I try to upload any media file it does not get saved in the "MEDIA_ROOT" location rather it get saved in "MEDIA_DIR".
How to serve media_root and media_dir in production?(/var/www/venv)
Static files are functional.
Hierarchy:
/var/www/ ----->media-root
>static-root
>venv--->src--->manage.py
>media
>static
>other apps & settings
In production mode my django project1 working fine.
settings.py
DEBUG = False
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR,'mysite' ,'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'mysite', "static"),
'/var/www/static/',
]
I ran this project in localhost:8000
And my different project(project2) which is running into localhost:8001
I want to show project1's home page in project2 using iframe or embed
but project1's static files not working here.
try with:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'/var/www/static/',
)
and set debug to true, if you set debug to false then run manage.py --insecure. The reason: If you set debug to true then you server will take care the staticfiles and not the Django server
I'm new to Django. First I'll explain my issue and my logic. I want to make my filepaths RELATIVE as opposed to the ABSOLUTE they are now so I can work on my laptop and PC and have everything show up as is.
I know I'll have to alter MEDIA_URL in the settings.py below. Does
MEDIA_URL = os.path.join(BASE_DIR, 'articles/static/articles/media/')
STATIC_URL = os.path.join(BASE_DIR, 'articles/static/')
make sense? I mean logically to me its saying from BASE_DIR which prints to C:\Users\kevIN3D\Documents\GitHub\articleTestProject\articleTestSite, would step into articles/static/articles/media/ or does the fact that
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
is abspath(...) change everything?
settings.py
"""
Django settings for articleTestSite project.
Generated by 'django-admin startproject' using Django 1.8.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
MEDIA_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/static/articles/'
MEDIA_URL = '/media/'
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2u#9=qkari39(465g+u!2t7*9tt_pdv)%155jdgxnki5#jujje'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'articles',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'articleTestSite.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 = 'articleTestSite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC-5'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/'
STATIC_URL = '/static/'
urls.py
urlpatterns = [
url(r'^articles/', include('articles.urls')),
url(r'^$', HomePage.as_view(), name='home'),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_ROOT)
My issue is when I try to use '/' in my declaration it prints out the filename as 'C:\Users\kevIN3D\Documents\GitHub\articleTestProject\articleTestSite\articles/static/' so it uses incorrect slashes for myself.
How do I go about making my page have relative filenames? I want to be able to work on this between my laptop and my PC, but as it currently stands I can only work on it on my PC because all the filenames are absolute and just give me broken links on my laptop.
Here is a link to the repository Github Repository. Please any help would be appreciated, I'm completely stumped. If someone could get that working, with images and custom CSS and then kind of walk me through what you did. Its my first time trying to distribute a Django file to more then just the local host.
Let me explain settings:
Right wave to separate the path
Pass every folder to the os.path.join() method. Example:
os.path.join(BASE_DIR, 'articles', 'static')
STATIC_URL, MEDIA_URL
This variables are passed to your template context proccessor (if you use django.template.context_processors.media) without any changes. This variables are used to make client-side links to your static and media content.
You should set your STATIC_URL and MEDIA_URL manually. Like that:
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
Then you can use them in templates:
<img src="{{STATIC_URL}}img/logo.png">(...)
STATIC_ROOT, MEDIA_ROOT, STATICFILES_DIRS
STATIC_ROOT is used to bring all your static files in one place by collectstatic command (if you're using django simple server, you shouldn't care about it).
STATICFILES_DIRS is the folder when your django simple server (manage.py runserver command) gets files to serve. If you will run collectstatic command, evety file from every dir listed in STATICFILES_DIRS copied be moved in STATIC_ROOT.
MEDIA_ROOT is the folder where Django will hold user-uploaded files (ImageField, FileField)
You should use absolute path in STATIC_ROOT, MEDIA_ROOT, STATICFILES_DIRS:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'example', 'static', 'folder',)
Please note. If you have static folder in your app folder and your app is listed in INSTALLED_APPS this folder will be added to your STATICFILES_DIRS automatically.
Serving static and media files during development
It's really simple just add this lines after your urlpatterns:
urlpatterns = patterns('',
... your patterns...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And don't forget to include settings:
from django.conf import settings
Serving on production server
Run collectstatic command to bring all your static content in one place.
Server your static and media folder using your apache/nginx/etc server:
Alias /media/ /path/to/your/media
Alias /static/ /path/to/your/static/
So I think I figured it out,
I was looking at things far to high-level so to speak. When in reality the answer was just simple. After giving myself a
print(BASE_DIR)
and having it spit out C:\Users\kevIN3D\Documents\GitHub\articlesTestProject\articleTestSite I realized all I needed to do was append that to my different ROOT variables.
MEDIA_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/static/articles/'
was my old MEDIA_ROOT, this was clearly absolute. BUT from this I can see that it shares that same filepath as BASE_DIR, so I went from there and
MEDIA_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/static/articles/'
became
MEDIA_ROOT = os.path.join(BASE_DIR, 'articles/static/articles/').replace('\\', '/') #run replace to convert UNIX slashes on Windows slashes
I had to do the same thing for STATIC_ROOT
STATIC_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/'
became
STATIC_ROOT = os.path.join(BASE_DIR, 'articles')
I deployed my final working project to heroku.
The problem is only html content loading, Static files are missing. I tried lots but missing something. Can you spot the error.
# .. settings.py
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(BASE_DIR, 'static/'),
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
You must give the STATIC_ROOT ...(It can not be empty). Because the "HEROKU", always run 'collectstatic' while deploying.. and it creates the folder like as 'static files' and all the process running by the way of this folder contents.
I'm trying to deploy my Django application at webfaction but the path to TEMPLATE_DIRS in settings seems to be incorrect.
My project is located under webapps/django/edmhunters
My settings.py file is under webapps/django/edmhunters/edmhunters
My template folder is at webapps/django/edmhunters/templates
In my settings.py file this is what i'm doing
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
But I still get TemplateDoesNotExist error. What would be the correct path?
Your templates directory is one more level up, you can do as below to get parent directory and use it to get templates directory.
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.normpath(os.path.dirname(SETTINGS_PATH))
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, '../templates'), )
use this instead of
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'), )