Django - Found another file with the destination path django - during deploying app - python

trying to deploy my app to uwsgi server
my settings file:
STATIC_ROOT = "/home/root/djangoApp/staticRoot/"
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/home/root/djangoApp/static/',
]
and url file:
urlpatterns = [
#urls
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and if I try to execute the command:
python manage.py collectstatic
Then some files are okay ( admin files ), but I see an error next to files from static folder. The error is like:
Found another file with the destination path 'js/bootstrap.min.js'. It
will be ignored since only the first encountered file is collected. If
this is not what you want, make sure every static file has a unique
path.
and have no idea what can I do to solve it.
Thanks in advance,

The two paths you have in STATICFILES_DIRS are the same. So Django copies the files from one of them, then goes on to the second and tries to copy them again, only to see the files already exist.
Remove one of those entries, preferably the second.

do you have more than one application?
If so, you should put any file on a subdirectory with a unique name (like the app name for example).
collectstatic collects files from all the /static/ subdirectories, and if there is a duplication, it throw this error.

Related

Django Found another file with destination path

I am trying to deploy the app on GCP but when I run the following command:
python manage.py collectstatic
It returns:
Found another file with the destination path
'admin\css\autocomplete.css'. It will be ignored since only the first
encountered file is collected. If this is not what you want, make sure
every static file has a unique path.
Found another file with the destination path 'admin\css\base.css'. It
will be ignored since only the first encountered file is collected. If
this is not what you want, make sure every static file has a unique
path.
And many other like this.
Here's my Settings.py
STATIC_URL = 'https://storage.googleapis.com/yantra-packs/static/'
# STATIC_URL = '/static/'
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), ]
Where is it that I am providing two paths for the static files?
The findstatic command will help to debug.
The default value of STATICFILES_FINDERS is:
[
'django.contrib.staticfiles.finders.FileSystemFinder', # finds files stored in the `STATICFILES_DIRS` setting.
'django.contrib.staticfiles.finders.AppDirectoriesFinder', # finds files stored in the 'static' subdirectory of each app.
]
Hence, even though you only have:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), ]
AppDirectoriesFinder will find files stored in the static subdirectory of each app.
Also, you might want to look at the Django's official documentation describing the collectstatic command.

Django is searching wrong statics directory

When i run django on uwsgi with;
uwsgi --http :8081 --module proj1.wsgi
It gives below error when i open start page from browser:
Not Found: /accounts/login/static/css/style.css
But my settings.py is :
STATIC_URL = 'static/'
STATIC_ROOT = '/home/proj1/static/'
STATICFILES_DIRS = (
'/home/proj1/staticorj/static/',
)
When i make collectstatic, it copies files to static root without problem.
I dont understand why does it look for /accounts/login.
It should look in /home/proj1 directory for static dir.
So the browser opens the page but without serving static files.
Statics operation should be like this.
First configure settings.py like below :
STATIC_URL = 'static/'
STATIC_ROOT = '/home/proj1/static/'
STATICFILES_DIRS = (
'/home/proj1/staticorj/static/',
)
Put statics files that you need in /home/proj1/staticorj/static/
Then run :
python manage.py collectstatic
This will carry /home/proj1/staticorj/static/ files to /home/proj1/static/ directory, and now django will use /home/proj1/static/ directory for static source.
If you will not make any statics changes set as below with commenting.
STATIC_URL = '/static/'
#STATIC_ROOT = '/home/proj1/static/'
#STATICFILES_DIRS = (
# '/home/proj1/staticorj/static/',
#)
And run your server. Better use uwsgi and nginx like apps to serv. In production files will be served. Also use new version browser.

Django can't find my static files after changing to production settings and back

I've been trying to deploy my site this weekend and have thus been meddling with the settings. One of the unpleasant surprises while doing this has been that my static files have seemingly stopped working on my site. My CSS files and javascript files don't work anymore, as if they aren't found by the site. The only thing I can remember doing with regards to static files was inserting this into settings.py:
# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# The URL to use when referring to static files (where they will be served from)
STATIC_URL = '/static/'
I removed these settings and replaced them with the original
STATIC_URL = '/static/'
but alas, the problem remains. Why isn't Django finding my static files anymore?
PS: As I'm a noob, I don't know exactly what is relevant from my project for you guys to see, but do let me know and I shall provide additional info.
Okay, update your settings.py as below:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_my_proj"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
Now, that you have done that make dir called static_my_proj, at the same level where your manage.py file is.
Okay, now create new dir called static_cdn just one level up, i.e. one level up to manage.py file.
Now, create dir called static_root inside static_cdn and that is all you have to do. Make sure you run python manage.py collectstatic
NOTE:
static_my_proj is for development or DEBUG = True and static_cdn is for production or DEBUG = True. handy, right?
Also, you can add media file the same way, just add,
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")
and make new dir called media_root under static_cdn.
EDIT:
Copy all your admin files to static dir manually.
If you have virtualenv than go to <virtualenv-dir>/lib/python3.6/site-packages/django/contrib/admin/static and copy dir named admin.
It should work even if DEBUG=False
Hope this helps.

Django. Using .ini files in code folder

I wanted to have a .py file using a ConfigParser class to recover information from a .ini file (containing URLs for a 3rd party REST service) Until now this worked since it was finding the file, but now it's not. App structure is:
app
folder
file.py
urls.ini
I assume this is due to my static files settings, which are as following:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
This is because I wanted to mantain settings that were compatible for both local development and web production escenarios (for which I could probably test with code in the web) for Heroku. I wanted to make it so I didn't have to change much stuff.
Thanks in advance.
If you're trying to read some files, you have to get the route.
If you have your static folder in your Django folder it could be easy to get your .ini file doing this
ini = open("%s/folder/yourfile.ini" % BASE_DIR, "r")
And then read your file lines.
In my opinion using a .py file for configuration it's easier than .ini file, and you can have many of them and load depending of a settings.py parameter for example.
Edit: for using Django config file variables
from django.conf import settings
BASE_DIR = settings.BASE_DIR

STATIC_URL issue in django 1.5.4

I wanted to enable serving static files through django 1.5 builtin development server. I came across a strange problem.
If I make a request to my static file using localhost:8000/static/staticstyle.css then it responds me with 404 not found. But If I make a request to the same file without the value mentioned in the STATIC_URL and add the url pattern as "url(r'^anyval_other_than_mentioned_STATIC_URL/', django.views.static.serve,{'document_setting':settings.STATIC_ROOT}), ", then it responds me with 304 , which is conditional get.
I have collected all the static files in the static directory setup in the settings file and enabled all the context processor required for the template.
For production I have used nginx to serve the static file so there is no problem.
My settings.py looks like
STATIC_ROOT = APPLICATION_PATH+"/static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('vendors',APPLICATION_PATH+'/vendors'),
('admin/assets/',APPLICATION_PATH+'/templates/admin/assets'),
# 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.
)
My application urls.py looks like
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Any hints would be highly appreciated.
Thank you .
please read this first
https://docs.djangoproject.com/en/1.5/howto/static-files/
There are some settings related to static files serve
The list of finder backends that know how to find static files in various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Absolute path to the directory static files should be collected to
STATIC_ROOT = os.path.join(APPLICATION_PATH, '..', 'static')
URL prefix for static files.
STATIC_URL = '/static/'
You can specify Additional locations of static files
STATICFILES_DIRS = (
os.path.join(APPLICATION_PATH, 'vendors'),
os.path.join(APPLICATION_PATH, '/templates/admin/assets'),
)
There is special application for static serve in django contrib
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
After You have configured your project run collectstatic command
python manage.py collectstatic
This will copy all files from your static folders into the STATIC_ROOT directory.

Categories