I am trying to deploy my Django project on the server. But, when I use it, the static file on Django can not be read correctly
I deploy my project on Debian server. The static file of course in same server, I have succeeded in deploying my project. But static files like css still can not appear in my project
This is my settings files:
"""
Django settings for akun project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(PROJECT_ROOT)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '8g*v#sf1i0y#+#5jyy$kk)wlixu*9yo(t$&1n%59ip*391sy#u'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'simofa',
'accounts',
)
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',
)
ROOT_URLCONF = 'akun.urls'
WSGI_APPLICATION = 'akun.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'pgina',
'USER': 'root',
'PASSWORD': '123',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Jakarta'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
# template location
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(PROJECT_ROOT), "static", "templates"),
'/home/boss/kantor/akun/templates/',
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(PROJECT_DIR),"static","static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(PROJECT_DIR),"static","media")
STATICFILES_DIRS = os.path.join(os.path.dirname(PROJECT_DIR),"static","static"),
i'm trying to change STATIC_URL='/static/' to the url STATIC_URL='http://www.url.com/my_project/static'
but the result still doesn't appears
When i try in my localhost, it works properly.
how is the solution ?
In production you need first to define STATIC_ROOT and then run collectstatic in order to have your static files collected there.
After running collectstatic you should be able to cd to the dir associated to STATIC_ROOT and see the files.
EDIT: the code below should be added in he Apache conf file, not in the Django settings
Finally if you are using Apache (and you are serving the files from the same server where you are running the Django app) you will need to serve the path of the STATIC_ROOT under the url defined in STATIC_URL, for example assuming STATIC_URL is /static/:
Alias /static/ /path/to/mysite.com/static_root_directory/
and then set permissions:
<Directory /path/to/mysite.com/static_root_directory>
Require all granted
</Directory>
PS you didn't provide many details about your environment (server, static on same server or not) so I had to make assumptions. If you provide more details I'm happy to help.
Static files dirs should be a tuple
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Related
This is the settings.py file. As you can see, both debug and template debug are set to False. Yet, if I try to trigger a 404 error, I don't get a regular 404 error or the one from my 404.html file. The 404 error that it returns is the one from Django where it says :
"You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page."
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'this is a secret'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['mywebsite.com','my website\'s ip']
# Application definition
INSTALLED_APPS = (
'myapp',
'django.contrib.admin',
'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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'django_project.urls'
WSGI_APPLICATION = 'django_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/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.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_ROOT = '/home/django/django_project/django_project/static'
STATIC_URL = '/static/'
Help!
(just to mark that this question is answered - by #e4c5)
summary:
if your django application is running as a separate daemon (manage.py runfcgi, gunicorn, uwsgi etc.), every time you change source file (this includes settings.py) - application need to be restarted
regarding templates - this depends how you load them - if you use any form of cache - you should reload daemon, without cache - it should be reloaded automatically
make sure that your date and time is always set properly, sometimes reported errors include that after code upgrade application is still running old one - in most cases there is an issue with the dates - compiled .pyc files are newer than your source .py ones, ./manage.py clean_pyc after deployment is a good thing to do.
I have created a django-rest project so that a user can register using email and subsequently user can activate the account using email link. The user authentication for rest server is working fine.
However, I want to create a ejabberd server for an instant messaging apps. I want to use external authentication method in ejabberd so that ejabberd can use the django project data.
Hence as per the guide line in Django-xmpp I am trying the following.
I have change the lines in ejabberd.cfg as
{auth_method, external}.
{extauth_program, "/home/...../ejabberd_auth.sh"}.
After that I have created a file name ejabberd_auth.sh which contains the following
#!/bin/bash
cd /home/------/src #(my django-rest project folder/location of manage.py)
python manage.py ejabberd_auth $#
Then I changed the setting.py in my django project. The settings file looks like
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-------'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.staticfiles',
# Rest Framework app
'rest_framework',
'rest_framework.authtoken',
# Internal Apps
'data',
'django_gravatar',
'xmpp',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
'rest_framework.permissions.DjangoModelPermissions'
)
}
ROOT_URLCONF = 'ndMyFirst.urls'
WSGI_APPLICATION = 'ndMyFirst.wsgi.application'
XMPP_DOMAIN = 'localhost'
XMPP_BOSH_SERVICE_URL = 'localhost:5280/http-bind'
# Database
# https://docs.djangoproject.com/en/1.6/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.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'`
Then I started ejabberd server. When I am trying to login in 127.0.0.1:5280/admin unauthorized response received. I have also tried to register a user using ejabberdctl register admin localhost password and not allowed message received.
I do not have any idea the external authentication works.
What should I do so that the external authentication works.
I've been trying to deploy the static .css files and stuff for a website my classmates and I have been doing. Right now we have kind of crap styling and stuff so we decided to use some code from Bootstrap to make it looks nice.
After editing my settings.py and doing python manage.py collectstatic, the static files were put into the static/ folder. Upon using the runserver command, none of the styling changes have been made. I've been looking on Google for a while but I have not discovered the solution.
I want to get it working on my local machine before I push it to PythonAnywhere.
Any help will be appreciated.
Here is my settings.py file
"""
Django settings for Dreadnaught project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = GOTCHA!
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'TTT'
)
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',
)
ROOT_URLCONF = 'Dreadnaught.urls'
WSGI_APPLICATION = 'Dreadnaught.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Dreadnaught',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '',
'PORT': '',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = ()
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (TEMPLATE_PATH,)
If you want to serve your css and js files in production mode you must change Debug true to false.
DEBUG=False
For reference STATIC
I have deployed my django project in pythonanywhere without any databases required, I am getting the error- DATABASES is improperly configured . The link to my deployed project is -http://drchitradhawle.pythonanywhere.com/
My setting.py file is-
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'fi+9_egiio(7l6xvbgk%o=!k(ktn3!ywhc4+p_6^57j4yvl0tp'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webpage',
)
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',
)
ROOT_URLCONF = 'website.urls'
WSGI_APPLICATION = 'website.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_ROOT = "/home/DrChitraDhawle/website/webpage/static"
STATIC_URL = '/static/'
STATICFILES_DIR = (
('assets', '/home/DrChitraDhawle/website/webpage'),
)
#
#STATICFILES_DIR = [os.path.join(BASE_DIR, '')]
MEDIA_URL = 'http://localhost:8085/media/'
Django needs a database to store information such as session/cookie info. So even if you don't use the database for your own website stuff, you still need one.
Fortunately, just using the default sqlite settings, and then running ./manage.py syncdb should be enough to get everything working for now.
Although you don't want to use database in your project but still you have to give database defination as #conrad said. So you can use sqlite3, a file based database that does not require setting up a database account and password.
Set up your database setting as -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/home/<username>/<ProjectName>/db.sqlite',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': ''
}
}
After that run the below command in console -
python ./manage.py syncdb
Now you will be asked for password, username and email-Id for creating superuser for database.
And its all done!
I've successfully deployed a live Django app on Heroku for the first time. However, I didn't realize that Heroku doesn't store media files - I want the admin to be able to upload images to the app - so I am trying to setup Amazon S3. This is my first time doing this setup.
1)My settings.py file includes this...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': "os.path.join(BASE_DIR, 'db.sqlite3')", # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
MEDIA_ROOT = '/media/'
MEDIA_URL = S3_URL + MEDIA_ROOT
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# 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',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'listing.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'listing.wsgi.application'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
'noticeboard',
'django_extensions',
'south',
'PIL',
'storages',
'boto',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
ASW_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
S3_BUCKET_NAME = os.environ['S3_BUCKET_NAME']
STATICFILES_STORAGE = 'storages.backends.s3boto.s3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.s3BotoStorage'
S3_URL = 'http://s3.amazonaws.com/%s' % S3_BUCKET_NAME
2) I've updated my requirements.txt file to include the boto and storages modules
3) I've setup my Heroku config file to include my bucket name, AWS Key and secret key.
This is the error I'm getting when I upload an image from the admin, it seems as if it's still trying to access the files on Heroku and not S3:
Exception Type: OSError
Exception Value:
[Errno 30] Read-only file system: '/Users'
Exception Location: /app/.heroku/python/lib/python2.7/os.py in makedirs, line 157
Python Executable: /app/.heroku/python/bin/python
Python Version: 2.7.4
I've read a few blog posts about this that say the same thing but I'm stuck as to why this isn't working!