pythonanywhere cant find files in me media folder
But I have this file!
roots in my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/home/dimabytes/my-first-blog/static'
MEDIA_ROOT = '/home/dimabytes/my-first-blog/media'
MEDIA_URL = '/media/'
me web page at pythonanywhere
The problem was that I automatically displayed the working directory in the folder with my name. It was necessary to finish the path of the project
It's because you have your path configured incorrectly, either when you call for the media or your MEDIA_ROOT and MEDIA_URL, most likely the former. If you look at your pythonanywhere file storage and your error output, it's looking for the file in media/static/<file_here>. Your targeted file is in /media/file/<file_here>.
Related
I have my static files in sub_app directory. I will name the folder sub_app where view.py, model.py are located. And when I run python manage.py findstatic it returns sub_app\static folder.
I have another folder name as main_app, where settings.py file is located. These two folders and manage.py file is located in a root folder.
I have no issue when DEBUG=True, but when I run DEBUG=False, I got following warning
venv\lib\site-packages\whitenoise\base.py:115: UserWarning: No directory at: root\static\
warnings.warn(u"No directory at: {}".format(root))
Here are static files settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Where should my static files?
You can put your static files anywhere, as long as you let your application know where they are. You have it configured that
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
but you say that you have your static files in sub_app\static. The error is telling you that it's checking the root folder. You need to change your configuration to
STATIC_URL = '/sub_app/static/'
STATIC_ROOT = '/sub_app/static'
Create a directory named static inside the app directory where the "view.py, model.py are located."
Inside static you need to create another directory with the same name as of the app.
Inside that folder you can create your static files which include anything like your css files
Just add this line in the settings.py of your app
STATIC_URL = '/static/'
As you mentioned sub_app
you should create sub_app/static/sub_app directory.
django.core.exceptions.ImproperlyConfigured: You’re using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
Having this error despite me having set the STATIC_ROOT:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Check the static file root. If it's on correct location. For heroku try to put that file on git. I am assuming it's a blank directory so it's not being pushed to git repo. Put .keep kind of file and add/commit and try. Hope it will solve your issue.
While I run python manage.py collectstatic
It says You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
This is my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static'),
]
How can I solve this issue?
You need to add STATIC_ROOT as a parameter to your settings.py.
Here is how it works with those static files:
In STATICFILES_DIRS you tell django where to go and look for static files. This not primarily to serve them, but to collect them! The point is that on a productive server, django is not supposed to server static files, this task should be handled by the web server. Check the docs for details. The idea is that you run the command python manage.py collectstatic and django will go through all the paths in STATICFILES_DIRS, collect the static files and put them... - exactly! - into the STATIC_ROOT folder.
At this point 2 things should become clear:
1. You need to provide STATIC_ROOT in your settings.py.
2. The path provided in STATIC_ROOT cannot be part of STATICFILES_DIRS, as this would mean django would need to put the folder into itself.
Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here is more from the docs on that. Nevertheless, the STATIC_ROOT parameter is always required.
Hope that helped to clarify things. Happy coding! :)
STATICFILES_DIRS tells django where to look for files to collect when using the collectstatic command
STATIC_ROOT="../my_real_static_folder" will tell django to put the static files it collects there and the static_url will get files from there.
... however be warned serving static files through django only works if debug is True (in settings.py) and is strongly discourages in production environments
Set STATIC_ROOT = 'path to ur static root directory' in settings.py.
Note that when you run collectstatic all files from ur static dirs are collected to the static root directory provided in static root path above. U can find more info about this in documentation here
In my settings file there is STATIC_URL = '/static/' and getting access to static files(css files) from the directory app/static/css/filename . If I add this to the setting file
MEDIA_URL = '/media/' is it possible to access the media files from app/media/filename? There is no MEDIA_ROOT or STATIC_ROOT in my settings.py.
The MEDIA_URL is meant for files uploaded by a user. These files are not checked in, so you shouldn't have a media directory inside your app.
For media files to work, you must set MEDIA_ROOT in your settings. See the docs for instructions how to serve media files.
What are the differences of these three static url?
I am not sure if I am right, I am using the MEDIA_ROOT to store my uploaded photos (via models.ImageField())
However, I created a JS script to my admin and in admin.py. I defined the media as below:
....
class Media:
js = ('/admin/custom.js', )
and my settings.py:
....
STATIC_ROOT = "/home/user/project/django1/top/listing/static"
and I added the custom.js to STATIC_ROOT/admin/custom.js, but it is not working. Throwing 404 not found error.
And then I change the STATIC_ROOT to STATICFILES_DIRS, and it works!!
....
STATICFILES_DIRS = "/home/user/project/django1/top/listing/static"
So, I am not understand what is going on here. In fact, I just don't understand what is the difference between STATIC_ROOT and STATICFILES_DIRS.
Currently I am testing Django in my machine via virtualenv, not deployed yet, is it the reason STATIC_ROOT not working??
Development
STATIC_ROOT is useless during development, it's only required for deployment.
While in development, STATIC_ROOT does nothing. You don't even need to set it. Django looks for static files inside each app's directory (myProject/appName/static) and serves them automatically.
This is the magic done by manage.py runserver when DEBUG=True.
Deployment
When your project goes live, things differ. Most likely you will serve dynamic content using Django and static files will be served by Nginx. Why? Because Nginx is incredibly efficient and will reduce the workload off Django.
This is where STATIC_ROOT becomes handy, as Nginx doesn't know anything about your Django project and doesn't know where to find static files.
So you set STATIC_ROOT = '/some/folder/' and tell Nginx to look for static files in /some/folder/. Then you run manage.py collectstatic and Django will copy static files from all the apps you have to /some/folder/.
Extra directories for static files
STATICFILES_DIRS is used to include additional directories for collectstatic to look for. For example, by default, Django doesn't recognize /myProject/static/. So you can include it yourself.
Example
STATIC_URL = '/static/'
if not DEBUG:
STATIC_ROOT = '/home/django/www-data/example.com/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
You can find these settings in the Django documentation. Here are my own definitions and quotations from the documentation:
MEDIA_ROOT is the folder where files uploaded using FileField will go.
Absolute filesystem path to the directory that will hold user-uploaded files.
STATIC_ROOT is the folder where static files will be stored after using manage.py collectstatic
The absolute path to the directory where collectstatic will collect static files for deployment.
If the staticfiles contrib app is enabled (default) the collectstatic management command will collect static files into this directory. See the howto on managing static files for more details about usage.
STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.
This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.
In your settings, you should have:
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# Make a tuple of strings instead of a string
STATICFILES_DIRS = ("/home/user/project/django1/top/listing/static", )
...where:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
as defined in the default Django settings.py now.
Difference between STATICFILES_DIRS and STATIC_ROOT
The STATICFILES_DIRS can contain other directories (not necessarily app directories) with static files and these static files will be collected into your STATIC_ROOT when you run collectstatic. These static files will then be served by your web server and they will be served from your STATIC_ROOT.
If you have files currently in your STATIC_ROOT that you wish to serve then you need to move these to a different directory and put that other directory in STATICFILES_DIRS. Your STATIC_ROOT directory should be empty and all static files should be collected into that directory.
MEDIA_ROOT where media files ,all uploaded files goes.
Example : Images, Files
class Media:
js = ('/admin/custom.js', )
but it is not working. Throwing 404 not found error.
The 404 error is in part because of the leading slash in the file path.