I know this question has been asked many times but none of the solutions seems to work for me. I just started using django and I'm having issues trying to get the CSS for the admin panel to show when I use runserver.
When I'm in: localhost:8000/admin/ none of the CSS shows up
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/dashboard.css" />
That is what is shown in the in the HTML of the admin page, which those URL's it links to ends up becoming http://localhost:8000/static/admin/css/base.css which doesn't link to anything.
In settings.py I have
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'codeapp',
'django.contrib.admin',
'django.contrib.admindocs',
)
STATIC_ROOT = '/Users/datboitom/Sites/Codebase/codebase/codeapp/static/'
STATIC_URL = '/static/'
I'm just not too sure what else to do. If can help me to fix this issue that would be great. I've ran python manage.py collectstatic and the static files are in /Users/datboitom/Sites/Codebase/codebase/codeapp/static/ folder. There just doesn't seem to be any link between these files and whats trying to be loaded. One is trying to load off the localhost url and not the path of where its located on the computer.
Just in case someone run into this error again. While in Debug=False mode, Django won't serve static files for you anymore. Your production server should take care of that. But, if you still want to test your app locally in debug=false you should run the devserver in insecure mode:
manage.py runserver --insecure
Taken from here: Why does DEBUG=False setting make my django Static Files Access fail?
What is your setting like your server?
According to the documentation, you should not write anything at STATIC_ROOT
Leave STATIC_ROOT blank and instead, then, put the path in STATICFILES_DIRS.
Hope this helps
I guess you are missing the following in your settings.py file
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Config ERROR
in setting.py, you should add 'django.contrib.staticfiles' in INSTALLED_APPS
the codes show:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
......
OK,try it.
I had this problem just recently after changing dev machines.
It turned out that my new machine wasn't set to run django apps in debug mode locally, so adding
DEBUG = True
To my dev machine's settings.py sorted it.
Related
I am trying to deploy my Django project on Digital Ocean. I created my droplet and spaces on Digital Ocean and created a static folder to store my static files. I pulled my code from my github-repo. then I installed all requirements and tried to collect static files with command
python3 manage.py collectstatic
but it shows
Unknown command: 'collectstatic'
Type 'manage.py help' for usage.
what should I do here?
I checked my manage.py helper but it has no command as collectstatic
check,
compilemessages,
createcachetable,
dbshell,
diffsettings,
dumpdata,
flush,
inspectdb,
loaddata,
makemessages,
makemigrations,
migrate,
runserver,
sendtestemail,
shell,
showmigrations,
sqlflush,
sqlmigrate,
sqlsequencereset,
squashmigrations,
startapp,
startproject,
test,
testserver,
these are the commands in manage.py helper.
And my settings.py is the following
import os
from pathlib import Path
from decouple import config
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = config('DEBUG', default=False, cast=bool)
SECRET_KEY = config("SECRET_KEY")
ALLOWED_HOSTS = ["134.209.153.105",]
ROOT_URLCONF = f'{config("PROJECT_NAME")}.urls'
# 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',
'crispy_forms',
'accounts',
'adminn',
'student',
'union',
'chat',
'channels',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
]
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = config('AWS_S3_ENDPOINT_URL')
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = config('AWS_LOCATION')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
TEMP = os.path.join(BASE_DIR, 'temp')
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
BASE_URL = "http://134.209.153.105"
here, in static url AWS_STORAGE_BUCKET_NAME, AWS_S3_ENDPOINT_UR, AWS_LOCATION are following...
AWS_STORAGE_BUCKET_NAME=studentcricle
AWS_S3_ENDPOINT_URL=https://sfo3.digitaloceanspaces.com
AWS_LOCATION=studentcircle-static
Thank you for those who checked my question.
My problem is solved as I run the following code.
export DJANGO_SETTINGS_MODULE=mysite.settings
I found it from Django documentation.
But I still did not find out what was the real problem.
It was something about my settings file. or multiple settings files
so, if anyone know the details, please describe it here. or in personal.
I think you should uncomment STATIC_ROOT inside your settings.py file and try this:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Try with replacing STATIC_ROOT in settings.py file with the following:
STATIC_ROOT = '/static/'
and whenever you run the python3 manage.py collectstatic command, make sure you are in the base directory where the manage.py file is located and there is a folder named static.
First of all please check if there is a folder in base directory named 'static'; if yes, then make a little change in the following code, remove'/' after static:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Secondly, make sure you have registered all the apps in the installed apps menu.
If the problem still persists, then try to run the following command: python manage.py shell
It should let you know where the problem is, if it's in the settings.
Hi I am trying to deploy django app on Google app engine. My Django app works fine in the locally but in google app engine it is not working. I checked and found that issue is with my static files. My static files are not getting loaded in app engine.
********.appspot.com/static/youtube/about.css
Not Found
The requested resource was not found on this server.
Its been two days I have trying to follow answers on various forums but none worked for me. I have following in my settings.py
my settings.py code snapshot
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'youtube',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sitemaps',
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Just for your information, I tried changing STATIC_ROOT to 'static' but didn't work.
STATIC_ROOT = 'static'
STATIC_URL = '/static/'
My directory structure:
my project dir structure
My HTML file from app youtube, and app directory structure
about page html file snapshot
<link rel="stylesheet" type="text/css" href="{% static 'youtube/about.css' %}">
My App.yaml
runtime: python
env: flex
runtime_config:
python_version: 3.7
entrypoint: gunicorn -b :$PORT yt_analytics.wsgi
automatic_scaling:
min_num_instances: 1
max_num_instances: 2
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
I have run below command to test on local server and then deploy on Google app engine.
python manage.py runserver
python manage.py collectstatic
gcloud app deploy
When I run collectstatic command I see it is creating new folder named static inside my project root directory.. i.e. yt_analytics/static
Now I see I have two static directories in my project--
yt_analytics/static
yt_analytics/youtube/static
When I ran findstatic command I got:
python manage.py findstatic youtube/about.css
Output:
Found 'youtube/about.css' here:
/home/work/Mirror/Projects/django/ytwebsite/yt_analytics/youtube/static/youtube/about.css
Have exhausted all forums and answers before asking this question. I am unable to deploy my app on GAE. Your help is appreciated.
Adding below code in urls.py after #Ivan Starostin's comment and it worked for me.
from django.conf.urls import url, include
from django.views.static import serve
urlpatterns = [
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Thanks Ivan for your valuable comment.
I'm using Django + Nginx + uwsgi
Settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
]
if DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
else:
STATIC_ROOT = "/var/www/mysite/static/"
STATIC_URL = '/static/'
if DEBUG:
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
else:
MEDIA_ROOT = "/var/www/mysite/media/"
MEDIA_URL = '/media/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
After update a code (in DEBUG = True all works good, static files are loading correct) I'm using python manage.py collectstatic on DEBUG = False in production server and it's collect admin static, but it's not collecting static for "main" app.
In debug mode all working good.
Checked serving by Nginx - it's working correct.
Why "collectstatic" doesn't collect static from "main" app?
UPD:
I'm an idiot. Forgot about add directory with static files in git. Sry.
I've deployed a Django 1.8 on heroku and none of the static files getting found at all. I can't figure out what I'm doing wrong:
# settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app123'
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
APPEND_SLASH = False
I put all the static files into myproject/app123/static/(css|images|js) on localhost. When I run heroku run python manage.py collectstatic it doesn't copy anything:
$ heroku run python manage.py collectstatic
Running `python manage.py collectstatic` attached to terminal... up, run.7135
You have requested to collect static files at the destination
location as specified in your settings:
/app123/staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
0 static files copied to '/app123/staticfiles', 117 unmodified.
I am trying to set up django-toolbar in my production site, so just certain IPs can use it. I have followed the official documentation (install and quick setup), but I can't see the toolbar.
The app is installed:
$ yolk -l | grep toolbar
django-debug-toolbar - 0.11.0 - active
I have added the required settings:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.sitemaps',
'modeltranslation',
'djangocms_text_ckeditor',
'cms',
'mptt',
'menus',
'south',
'sekizai',
'debug_toolbar',
# more stuff...
)
INTERNAL_IPS = ('---.---.---.---',) # my current ip, taken from the server logs so I'm sure it's correct
STATIC_URL = '/static/'
DEBUG = True
There's no error message. I have also run collectstatic just in case. No success.
Any suggestion? Thanks :-)
EDIT: it seems that the order within INSTALLED_APPS matters. Moving 'debug_toolbar' right after all 'django.contrib.*' and before 'modeltranslation' and the others solved the issue.
Have you set the INTERNAL_IPS to what is in request.META['REMOTE_ADDR'] (second answer here)?
I had a similar problem, the toolbar wasn't displaying and I couldn't figure out why. But when I printed request.META['REMOTE_ADDR'] my IP wasn't what I thought it was! (because I was using port forwarding and virtual box).
I found out the reason. Within INSTALLED_APPS, 'debug_toolbar' must appear as soon as possible. I had placed some other applications before ('modeltranslation', 'cms', etc.). Placing it just after all 'django.contrib.*' and before the others works fine.