I´m trying to install bower on a django project I have.
I executed both:
npm install -g bower
and
pip install django-bower
if I writebower --version
I get:1.4
however on my django project when writing
py manage.py bower install
or
py manage.py bower_install
I get Unknown Command: 'bower'
This is my setting.py
"""
Django settings for agenda 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.join(os.path.dirname(__file__), ".."),
)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
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.
TEMPLATE_PATH,
)
PROJECT_DIR = os.path.abspath(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 = '#7j23xm3jv=(#gicejabv2ppa$063st+d#)2x^thld0(#!chwq'
# 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',
'modulo_agenda',
'schedule',
'djangobower',
)
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.locale.LocaleMiddleware',
)
ROOT_URLCONF = 'agenda.urls'
WSGI_APPLICATION = 'agenda.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/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.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
MEDIA_ROOT = os.path.join(PROJECT_DIR, "site_media")
ADMIN_MEDIA_PREFIX = '/media/'
MEDIA_URL = '/site_media/'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'djangobower.finders.BowerFinder',
)
BOWER_COMPONENTS_ROOT = os.path.join(PROJECT_ROOT, "components")
BOWER_INSTALLED_APPS = (
'jquery',
'bootstrap'
)
FIRST_DAY_OF_WEEK = 1 # Monday
Can anyone tell me what I´m doing wrong?
bower is installed on my computer but somehow I cannot get the manage.py script to recrognize it as a valid command. Maybe I forgot something on the manage.py?
Thanks for the help.
As referenced here: https://github.com/nvbn/django-bower/issues/34
./manage.py bower_install
Hope this works.
Related
at local when i accessed localhost:8000/admin, it worked....!!! but when i access https://realmento.herokuapp.com/admin/login/?next=/admin/ , i get message DoesNotExist at /admin/login/
Site matching query does not exist. i have deployed my project to heroku...
this is my setting.py :
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
location = lambda x: os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', x)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['*']
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'index',
'category',
'products',
'allauth',
'allauth.account',
'allauth.socialaccount',
# ... include the providers you want to enable:
'allauth.socialaccount.providers.bitly',
'allauth.socialaccount.providers.dropbox',
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
)
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',
'solid_i18n.middleware.SolidLocaleMiddleware',
)
SITE_ID = 1
SITE_ID = 2
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.i18n",
'realmento.context_processors.solid_i18n',
)
# default language, it will be used, if django can't recognize user's language
LANGUAGE_CODE = 'en'
# supported languages
LANGUAGES = (
('en', 'English'),
('ko', 'Korean'),
)
USE_I18N = True
LOCALE_PATHS = (
location('locale'),
)
CACHES = {
'default': {
'BACKEND':'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION':'127.0.0.1:8000',
}
}
###############################################################
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR,'templates'), os.path.join(BASE_DIR,'templates', 'allauth'))
#################################################################
ROOT_URLCONF = 'realmento.urls'
WSGI_APPLICATION = 'realmento.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
TIME_ZONE = 'UTC'
USE_L10N = True
USE_TZ = True
ADMINS = (('HOANGTHINH', 'rongbay054#gmail.com'), )
EMAIL_SUBJECT_PREFIX = "[Translation] "
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'hoangthinh2641995#gmail.com'
EMAIL_HOST_PASSWORD = '*************'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
LOGIN_REDIRECT_URL = '/realmento/intro'
LOGOUT_URL = "/accounts/login/"
SOLID_I18N_USE_REDIRECTS = False
SOLID_I18N_HANDLE_DEFAULT_PREFIX = False
SOLID_I18N_DEFAULT_PREFIX_REDIRECT = False
# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'realmento',
'USER': 'postgres',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '5432',
}
}
# DATABASE_URL=postgres:///postgres:root#localhost/realmento
DATABASES['default'] = dj_database_url.config(default = 'postgres://postgres:root#localhost/realmento')
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
So what's the problem? Thanks a lot for your help!
Remove 'django.contrib.sites' from your settings file.
It worked for me.
That's becaue /admin/login/ is not there, try opening
https://realmento.herokuapp.com/admin/ only.
or try opening
localhost:8000/admin/login/?next=/admin/
and see if it opens or not.
Did you run python manage.py collectstatic This will copy all files from your static folders into the STATIC_ROOT directory.
I know there are lots of these questions already but I haven't been able to find an answer. I'm trying to get my homepage to load but I have been getting this error:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.7.1
Exception Type: TemplateDoesNotExist
Exception Value:
base.html
Exception Location: /Users/user/.virtualenvs/screen_savers/lib/python3.4/site-packages/django/template/loader.py in find_template, line 136
Python Executable: /Users/user/.virtualenvs/screen_savers/bin/python
Python Version: 3.4.1
Python Path:
['/Users/user/Devspace/scren_savers/the_screen_savers',
'/Users/user/.virtualenvs/screen_savers/lib/python34.zip',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4/plat-darwin',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4/site-packages']
and can't seem to figure out why. I specified my TEMPLATE_DIR properly in my settings.py and made sure that the file exists where is says it does. I have also looked through all of these questions where people had the same problem but none of them solved my problem:
Django App template Loader, can't find app templates
Django template Path
Django can't find template
Django TemplateDoesNotExist?
Template does not exist
Django cant find templates
What really confuses me is that if I check the value of BASE_DIR and TEMPLATE_DIR it gives me:
/Users/user/Devspace/scren_savers/the_screen_savers
/Users/user/Devspace/scren_savers/the_screen_savers/templates
respectively which is exactly what I expect to see. It seems like it knows where the templates are but still can't see them. Any help would be greatly appreciated.
Also an additional, less important question:
I have seen BASE_DIR + '/templates' and os.path.join(BASE_DIR, "templates") suggested as the proper way to specify the TEMPLATE_DIR. Is one of these better than the other (if so why) or is this just a matter of personal preference?
Here is the view that I am trying to load:
from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import View
class home_page(View):
def get(self, request):
return render(request, 'base.html')
and here is my settings file:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIR = (
BASE_DIR + "/templates/",
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# 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',
'screen_savers',
)
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 = 'screen_savers.urls'
WSGI_APPLICATION = 'screen_savers.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_URL = '/static/'
You should change in file settings.py TEMPLATE_DIR to TEMPLATE_DIRS.
Getting:
ImproperlyConfigured at /admin/login/ You're using the staticfiles app
without having set the required STATIC_URL setting.
after I deployed the Django app to Heroku:
https://rtd2015.herokuapp.com/admin/
I'm also getting a 404 on the root URL: rtd2015.herokuapp.com.
Thanks
base.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from os import environ
import dj_database_url
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!
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, '../templates/'),
)
ALLOWED_HOSTS = ['.herokuapp.com', '.researchthroughdesign.org',]
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gunicorn',
'markdown_deux',
'storages',
'site_2015',
)
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 = 'app.urls'
WSGI_APPLICATION = 'app.wsgi.application'
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-gb'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
MARKDOWN_DEUX_STYLES = {
"STYLE": {
"extras": {
"code-friendly": None,
},
"safe_mode": False,
},
}
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
production.py
from base import *
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_STORAGE_BUCKET_NAME = 'rtd2015'
STATICFILES_STORAGE = 'app.settings.s3utils.StaticRootS3BotoStorage'
DEFAULT_FILE_STORAGE = 'app.settings.s3utils.MediaRootS3BotoStorage'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '../../srv/assets'),
)
STATIC_ROOT = 'http://rtd2015.s3.amazonaws.com/static/'
STATIC_URL = 'http://rtd2015.s3.amazonaws.com/static/'
MEDIA_ROOT = 'http://rtd2015.s3.amazonaws.com/media/'
MEDIA_URL = 'http://rtd2015.s3.amazonaws.com/media/'
I have recently started a Digital Ocean server with a pre-installed Django image on Ubuntu 14.04. I wanted to create an API, and have decided on the Django Rest Framework. I installed the Django Rest Framework exactly according to http://www.django-rest-framework.org/.
Here is what the tutorial site looks like when I access it on my server.
As you can see, it does not look like the site on the rest framework tutorial website. This is because of the fact that when I view the source code of my site, all of the /static/rest_framework/* files give me a 404 error.
Here is my settings.py file in the Django 'django_project' root directory.
"""
Django settings for django_project project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/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.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '7Vnib8zBUEV3LfacGKi2rT185N36A8svyq8azJLvNpv7BxxzMK'
# 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',
'rest_framework',
)
REST_FRAMEWORK = {
# Use hyperlinked styles by default.
# Only used if the `serializer_class` attribute is not set on a view.
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.HyperlinkedModelSerializer',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
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.postgresql_psycopg2',
'NAME': 'django',
'USER': 'django',
'PASSWORD': 'yj4SM6qcP0',
'HOST': 'localhost',
'PORT': '',
}
}
# 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/'
Can anyone help me fix this missing /static/rest_framework/ location error? If I am going to have an API for my application I would like it to be a good looking one.
Let me know if you need anything else to help you fix this, and thank you in advance for your help.
I have found the solution to my problem!
After much mind boggling research, I re-read this stack overflow question that didn't seem to help me the last time I took a look at it.
My new settings.py in my django_project folder now looks like this.
"""
Django settings for django_project project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/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.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'DwGCDqtcqzzGO2XK87u7bVSEUqHogZRFl4UdhkcCudSHxLUVvx'
# 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',
'rest_framework',
)
REST_FRAMEWORK = {
# Use hyperlinked styles by default.
# Only used if the `serializer_class` attribute is not set on a view.
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.HyperlinkedModelSerializer',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
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.postgresql_psycopg2',
'NAME': 'django',
'USER': 'django',
'PASSWORD': 'mpOQzpYFci',
'HOST': 'localhost',
'PORT': '',
}
}
# 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/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I now have a folder named 'static' right next to my settings.py file in my django_project folder with all necessary resources such as 'rest_framework' and 'admin'. I restarted gunicorn after this change and reloaded my web page and it worked!
Thanks to those of you who tried to help, you did lead me in the right direction and probably made this go by a lot faster.
First, You need to set static url and static root in django settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
Then collect all static files
python manage.py collectstatic
In my case I was relying on Gunicorn to run the server.
I tried updating my settings.py file by following the above threads but unfortunately nothing seemed to work for me.
In the end I scratched my head around DRF docs specially this part https://docs.djangoproject.com/en/dev/howto/static-files/
I managed to solve this problem by updating by urls.py as following.
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Then updating my settings.py as follows.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And now execute => python manage.py collectstatic
I did this tens of times and I was going insane over it. I had DEBUG = False.
If you're using Heroku to serve your website, try this link. It worked for me.
https://devcenter.heroku.com/articles/django-assets
In Summary:
Make a static folder if you don't have one already. (put it on the same level as your manage.py file)
Add the Python package to the folder (Copy this folder into the static folder --> C:\Python37_64\Lib\site-packages\rest_framework)
Add the code below to your project.
settings.py
...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
MIDDLEWARE_CLASSES = (
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
'whitenoise.middleware.WhiteNoiseMiddleware',
...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
requirements.txt
...
#add whotrnoise to requirements
whitenoise
I had the same issue when I started learning python. I fixed by making below changes in settings.py of my project
DEBUG = True
I was unable to get any of the above solutions to work on our webapp, but discovered that if the app can connect to an S3 bucket where it can access deployed static files, django rest_framework works pretty seemlessly (as discussed here). Here's the relevant code for our settings.py:
aws = pcfenv.get_service(label='aws-s3') # or however you are accessing your s3 bucket & credentials
if aws is not None:
keys = aws.credentials
AWS_ACCESS_KEY_ID = keys["access_key_id"]
AWS_SECRET_ACCESS_KEY = keys["secret_access_key"]
AWS_STORAGE_BUCKET_NAME = keys["bucket"]
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
DEFAULT_FILE_STORAGE = 'mysite.storage_backends.MediaStorage'
# static files
STATIC_URL = '/static/'
STATIC_ROOT = 'static/'
# local storage
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/uploads/'
You'll need to pip install the boto3 and django-storages dependencies for it all to work.
you need both:
REST_FRAMEWORK = {
# Use hyperlinked styles by default.
# Only used if the `serializer_class` attribute is not set on a view.
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.HyperlinkedModelSerializer',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
and
the
python manage.py collectstatic
with the relevant directories set up.
the latter without the former won't do the work.
In debug mode, no changes are required.
If you are in a production environment, you need to set the static files point, usually set in nginx
location /static {
alias /<your app>/static; # your Django project's static files - amend as required
}
I am trying to set up my django site to run on a web server. I am attempting to set it up to run using a mysql database. However, I get an error Operational Error: unable to open database file. Most resources on the internet point to this error being caused while attempting to use sqlite. I am not sure exactly what I am doing wrong. Here is the relevant portion of my settings.py file:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_SITE_URL = ...
# 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 = []
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'list_app',
)
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',
'django_cas.middleware.CASMiddleware',
'django.middleware.doc.XViewMiddleware',
)
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'django_cas.backends.CASBackend'
)
ROOT_URLCONF = 'list_site.urls'
WSGI_APPLICATION = 'list_site.wsgi.application'
CAS_SERVER_URL = ...
CAS_LOGOUT_COMPLETELY = True
CAS_REDIRECT_URL = BASE_SITE_URL + '/lists/index'
CAS_ADMIN_PREFIX = BASE_SITE_URL + '/admin'
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'maillists',
'USER': 'wasingej',
'PASSWORD': '...',
'HOST': '...',
'PORT': '3306',
}
}
# 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/'
for MySQL I use
'ENGINE': 'django.db.backends.mysql',
and for the DB name: just provide the name ("maillists") - not a(ny) path
You have defined the database ENGINE part with an error. For MySQL, it should be
ENGINE: 'django.db.backends.mysql',
Changing that will work