collectstatic doesn't ignore specified files - django

i'm having some trouble figuring out how to manage my scss files. Here's what my folder structure looks like:
manage.py
├── myWebsite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── projects
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── static
│   │   └── projects
│   │   └── css
│   │   ├── index.css
│   │   ├── scss
│   │   │   └── survey.scss
│   │   └── tribute.css
│   ├── templates
│   │   └── projects
│   │   ├── base.html
│   │   ├── documentation.html
│   │   ├── index.html
│   │   ├── portfolio.html
│   │   ├── product.html
│   │   ├── survey.html
│   │   └── tribute.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── requirements.txt
└── staticfiles
└── projects
└── css
└── scss
├── survey.css
└── survey.css.map
I'd like to keep my .scss files inside the static/{app_name}/css/scss folder of each app. Those are later compiled inside the staticfiles folder which is in the project root. That same folder is the root for collectstatic, which is run when deploying on the server (AWS EB)
02_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"
Since I'm using Django 2.2 it should be possible to ignore .scss files to be collected by collectstatic but doesn't seem to be working, I tried:
python manage.py collectstatic --noinput -i projects/static/projects/css/scss
EDIT: this is the kind of error I get:
Activity execution failed, because: usage: manage.py collectstatic [-h] [--noinput] [--no-post-process]
[-i PATTERN] [-n] [-c] [-l]
[--no-default-ignore] [--version]
[-v {0,1,2,3}] [--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback]
[--no-color] [--force-color]
manage.py collectstatic: error: unrecognized arguments: /projects/static/projects/css/scss
(ElasticBeanstalk::ExternalInvocationError)

I think you are pointing to the wrong path when you are using -i. You need to find the path from static directory root, not project root. For example if your static directory is:
STATICFILES_DIRS = [
os.path.join(APPS_DIR, "static"), # where projects is the apps directory
]
# OR if you are using django-cookie-cutter
# STATICFILES_DIRS = [str(APPS_DIR.path("static"))]
Then, update the code like this:
python manage.py collectstatic --noinput -i projects/css/scss

Following Django 2.2 Release Notes,
Added path matching to the collectstatic --ignore option so that patterns like /vendor/*.js can be used.
python manage.py collectstatic --noinput --ignore /projects/static/projects/css/scss

Related

Issue with static files when deploying Django app to Heroku

My project folder looks this:
├── Procfile
├── core
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   └── views.cpython-39.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── inspirationSources.txt
├── manage.py
├── package-lock.json
├── package.json
├── react-frontend
│   ├── README.md
│   ├── build
│   │   ├── asset-manifest.json
│   │   ├── favicon.ico
│   │   ├── index.html
│   │   ├── logo192.png
│   │   ├── logo512.png
│   │   ├── manifest.json
│   │   ├── robots.txt
│   │   └── static
│   ├── package-lock.json
│   ├── package.json
│   ├── public
│   │   ├── favicon.ico
│   │   ├── index.html
│   │   ├── logo192.png
│   │   ├── logo512.png
│   │   ├── manifest.json
│   │   └── robots.txt
│   └── src
│   ├── App.css
│   ├── App.js
│   ├── App.test.js
│   ├── assets
│   ├── components
│   ├── hooks
│   ├── index.css
│   └── index.js
├── requirements.txt
├── spotify
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── admin.cpython-39.pyc
│   │   ├── apps.cpython-39.pyc
│   │   ├── cluster.cpython-39.pyc
│   │   ├── credentials.cpython-39.pyc
│   │   ├── models.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   ├── util.cpython-39.pyc
│   │   └── views.cpython-39.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── cluster.py
│   ├── credentials.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   ├── models.py
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   ├── util.py
│   └── views.py
├── spotifycluster
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── tutorialSources.txt
When I deploy with git push heroku main it seems to be fine but when I open the app in browser using the complementary url, I get the following errors on screen (debug mode is on):
TemplateDoesNotExist at /
build/index.html
Request Method: GET
Request URL: https://nameless-taiga-02413.herokuapp.com/
Django Version: 3.1.7
Exception Type: TemplateDoesNotExist
Exception Value:
build/index.html
Exception Location: /app/.heroku/python/lib/python3.9/site-packages/django/template/loader.py, line 19, in get_template
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.9.4
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python39.zip',
'/app/.heroku/python/lib/python3.9',
'/app/.heroku/python/lib/python3.9/lib-dynload',
'/app/.heroku/python/lib/python3.9/site-packages']
Server time: Fri, 30 Apr 2021 10:08:26 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /app/react-frontend/build/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.9/site-packages/django/contrib/admin/templates/build/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/templates/build/index.html (Source does not exist)
I have the following settings in my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Also I have whitenoise in my middleware list
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
...]
The templates section in settings.py looks as follows
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'react-frontend')],
'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',
],
},
},
]
I expected that Heroku would be able to find the static folder based on the STATIC_DIRS variable but that doesn't seem to be the case. Any clue what's going on here?
So actually it was a really stupid mistake. react-frontend was a broken submodule and I needed to fix that by making sure .git/config didn't have submodules and removing react-frontend from the cache (No submodule mapping found in .gitmodules for path and missing .gitmodules file). Commit and pushed again and it worked...
if your DEBUG is set to False Django din't handle STATIC FILES. Heroku provide some configuration to serve STATIC FILES
STEP-1 : install whitenoise
$ pip install whitenoise
STEP-2 : check in settings.py whitenoise middleware by default it available in MIDDLEWARE if it's not that add it.
MIDDLEWARE = (
'whitenoise.middleware.WhiteNoiseMiddleware',
...)
STEP-3: add this in your settings.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Eclipse: unresolved import

Eclipse Neon (4.6.0).
PyDev for Eclipse 5.1.2.201606231256
I have created a Django project:
File / New / Project / PyDev Django project
Selected "Add project directory to the PYTHONPATH".
Now I have this folder structure.
(django_comments) michael#ThinkPad:~/workspace/formsets$ tree
.
└── formsets
├── db.sqlite3
├── formsets
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── settings.cpython-35.pyc
│   │   ├── urls.cpython-35.pyc
│   │   └── wsgi.cpython-35.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── home_page
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-35.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-35.pyc
│   │   ├── __init__.cpython-35.pyc
│   │   ├── models.cpython-35.pyc
│   │   └── views.cpython-35.pyc
│   ├── templates
│   │   └── home_page
│   │   └── home_page.html
│   ├── tests.py
│   └── views.py
└── manage.py
In prjoect properties in PyDev-PYTHONPATH at the tab Source Folders I have:
/${PROJECT_DIR_NAME}
In home_page/views.py I have created HomePageView. And in urls.py I would like to import it:
from home_page.views import HomePageView
The problem is:
1) HomePageView is underlined with red line. Error is Unresolved import: HomePageView.
2) Code completion is not working.
By the way, if I run the project, it works. That HomePageView shows what was expected.
Could you help me understand what have I done wrongly.
Try to add a path to the directory where your manage.py file is located. So I'm guessing it to add:
/${PROJECT_DIR_NAME}/formsets

Django App Deployed on AWS Elastic Beanstalk throws 404?

I am trying to deploy a sample application on AWS. My folder structure:
eb_django_app
├── .ebextensions
│   └── 01-django_eb.config
├── .elasticbeanstalk
│   └── config.yml
├── predictfare
│   ├── .gitignore
│   ├── db.sqlite3
│   ├── manage.py
│   ├── predictfare
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   ├── urls.py
│   │   ├── urls.pyc
│   │   ├── wsgi.py
│   │   └── wsgi.pyc
│   │  
│   └── templates
│   └── base.html
└── requirements.txt
My 01-django_eb.config:
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "predictfare.settings"
PYTHONPATH: "/opt/python/current/app/predictfare:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: "predictfare/predictfare/wsgi.py"
My config.yml:
branch-defaults:
default:
environment: faremagus-dev
group_suffix: null
global:
application_name: faremagus
default_ec2_keyname: aws-eb
default_platform: Python 2.7
default_region: us-west-2
profile: eb-cli
sc: null
Logs Show : Target WSGI script not found or unable to stat: /opt/python/current/app/predictfare/predictfare
Mostly followed this tutorial. When I run eb open I see 404 in the browser. What am I missing?

../manage.py makemessages -l zh_CN do not create file

I am using Django for a web app.Today I used command:
../manage.py makemessages -l zh_CN
to create the .po file.But nothing hanppend without printing the
processing locale zh_CN
Why?In my settins.py,all things about I18N was set.
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale'),)
Does anyone know how to deal?
Many thanks
And
django-admin makemessages -l zh_CN
is still the same result.
My app is 'enterprise' and 'myauth'.
My directories structure is:
.
├── Web
├── captcha
├── collected_static
├── demo
├── dict
├── enterprise
│   ├── migrations
│   ├── static
│   ├── templates
│   ├── templatetags
│   └── view
├── locale
├── logs
├── myauth
│   ├── form
│   ├── locale
│   ├── migrations
│   ├── static
│   ├── templates
│   └── views
├── static
│   ├── bootstrap
│   ├── d3
│   ├── img
│   ├── jquery
│   └── nvd3
└── templates

django serving static css files

I am relatively new to Django development.I have a css file inside a /static/css directory.
When I try to run the url no CSS is applied to my template. the python manage.py runserver window shows following error
[01/Jan/2013 20:00:40] "GET /home/prat/PROJECT_ROOT/SOURCE_ROOT/static/css/Style.css HTTP/1.1" 404 2207
Can someone please point me how to debug this. I have read multiple stackoverflow questions and added the following setting in my settings.py.
PROJECT_R = os.path.abspath(os.path.dirname(__name__))
PROJEECT_R = PROJECT_R + "../"
STATIC_ROOT = os.path.join(PROJECT_R, "static")
STATIC_URL = 'static/'
.
├── manage.py
├── README
├── SOURCE_ROOT
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── static
│   ├── css
│   │   ├── README
│   │   └── Style.css
│   ├── images
│   │   └── README
│   └── js
│   └── README
├── template
│   ├── base.html
Here's how I usually go about managing dynamic project root:
from os.path import dirname, realpath, join
PROJECT_ROOT = dirname(realpath(__file__))
And then further below, the static root:
STATIC_ROOT = join(PROJECT_ROOT, 'static/')
And then you reference static files like so:
{{ STATIC_URL }}css/Style.css
EDIT:
See the documentation for more information.