I have a form (model form) that sends data(such as images, title, etc.) with POST request.
In setting.py file, for MEDIA_ROOT, I tried 2 approach:
1- Forward slash "/" :
MEDIA_ROOT = [ BASE_DIR / 'static/images' ]
2- os.path.join():
MEDIA_ROOT =os.path.join(BASE_DIR, 'static/images')
When I use the first one(forward slash), I get the below error:
TypeError at admin/projects/project/...
_getfullpathname: path should be string, bytes or os.PathLike, not list
Do you know why I can't use the forward slash "/" for MEDIA_ROOT while this way it completely works for STATICFILES_DIRS?
MEDIA_ROOT [Django-doc] is a single path, so for example:
MEDIA_ROOT = BASE_DIR / 'static/images' # 🖘 no list
Related
I'm using Django and react with webpack
While setting up WEBPACK_LOADER in settings.py, I've faced the problem!
My file tree is like this.
My File Tree
Cebula4
- back
- manage.py
- front
- bundles
- MainPage.js (webpacked)
and
WEBPACK_LOADER = {
'DEFAULT' : {
'BUNDLE_DIR_NAME': '/front/bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'front/webpack-stats.json'),
}
}
I'd like to get a bundled(webpacked) file, MainPage.js but, Server said there is none at that url.
Please understand my low level of programming,
Could I ask what is the correct url that I can get exact MainPage.js?
Added part of my STATIC_URL and STATIC_ROOT
STATIC_URL = '/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
]
STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles')
When I make collectstatic, it doesn't work, and create static folder and collect into D:\static\.
My settings don't work correctly. Why?
Here are my settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'D:\DjangoProjects\mysite']
STATIC_ROOT = '/static/'
In Python, as in many languages, the backslash is the escape character. If you want a literal backslash, you need to escape it itself:
STATIC_ROOT = 'D:\\static\\'
or you can use the r'' notation for a literal string:
STATIC_ROOT = r'D:\static\'
However, the Django documentation says that you should be using forward slashes, even on Windows. It's not clear whether this also applies to STATIC_ROOT and MEDIA_ROOT, but it is for STATICFILES_DIRS. In that case, STATIC_ROOT would become:
STATIC_ROOT = 'D:/static/'
The 'D:\DjangoProjects\mysite' you have written is certainly an error, should be 'D:/DjangoProjects/mysite'.
Here is the error I receive when trying to load {{STATIC_URL}}img/memestatlogo2.jpg. Why is django not looking in STATICFILES_DIRS for img/memestatlogo2.jpg?
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/img/memestatlogo2.jpg
Using the URLconf defined in memestat.urls, Django tried these URL patterns, in this order:
^$
The current URL, static/img/memestatlogo2.jpg, didn't match any of these.
Here are relevant configurations form my settings.py
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('/home/ryan/Programming/OpenCV-2.4.2/msheroku/memestat/stats/')
The image is then located in
'/home/ryan/Programming/OpenCV-2.4.2/msheroku/memestat/stats/img/memestatlogo2.jpg'
STATICFILES_DIRS is only used when you execute the collectstatic command. Files are then copied into STATIC_ROOT which in the example above is unset.
You need to set STATIC_ROOT to a dir where you want your static files to be copied. This dir also holds your project-wide static files that are not part of any app like perhaps robots.txt, favicon.ico, your logo(s) and background images.
I am from PHP background and want to give path to
STATICFILES_DIRS
in settings.py and in windows I understand that I will need to give full path like:
D:/path/to/folder/project/static
I want to know that is there a way to give some path like "/static/" or can we give path like dirname(__FILE__) in PHP?
So that my path is not hardcoded. Also why it is physical path as webserver normally don't follow physical path for loading CSS files as it use http path? So why it is this way(physical path) in django? Can some one please explain.
thanks for everyone's effort in advance.
In my settings.py files, I always do
import os
ROOT_PATH = os.path.dirname(__file__)
Then you can do
STATICFILES_DIRS = [os.path.join(ROOT_PATH, 'static')]
The reason that STATICFILES_DIRS wants a filesystem path is that it needs to know where the files live in the operating system (so things like manage.py collectstatic are possible). The STATIC_URL setting is for webserver paths, and that's what it uses to show to the user in the admin or the {% static %} tag or whatever. STATICFILES_DIRS is server-side only and never shows up in any rendered templates or anything.
Find the settings.py file in the project,
Put:
STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'))
Change to:
STATICFILES_DIRS=[(os.path.join(BASE_DIR,'static'))]
Answer on your question is wrong. If you do like this, you'll get the next error:
ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?
My decision is add to top of settings.py file the next function:
import os
def look_folder_tree(root):
result = ()
for dir_name, sub_dirs, file_names in os.walk(root):
for sub_dir_name in sub_dirs:
result += (os.path.join(dir_name, sub_dir_name),)
return result
# Django settings for project.
PROJECT_DIR = os.path.dirname(__file__)
and call look_folder_tree function at:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = look_folder_tree(STATIC_ROOT)
My decision is allow to add all sub folders inside static directory.
In settings.py :
Import os (already done when you create a django project)
import os
You can just use STATICFILES_DIRS as a tuple, mind the trailing comma.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Or you can use the STATICFILES_DIRS as a list data-type, comma at the end is not required here:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Change this:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'))
To This:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Add a comma at last as,
ERRORS:
?: (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or list.
HINT: Perhaps you forgot a trailing comma?
your syntax is correct, I think you forget of importing os at the first line of code as import os.
If you are using newer versions of Django i.e Django 3.1, and your BASE_DIR is like this ...
BASE_DIR = Path(__file__).resolve().parent.parent
Then do this :-
STATICFILES_DIRS =[BASE_DIR / 'my_app/static',]
I'm doing some local development using Django and Satchmo. When I upload product images locally via the admin, the path to the image shows up as an absolute path, complete with drive letter, rather than the proper relative path.
Stranger still, Satchmo saves both the original image and the thumbnails it generates in both me /media/ directory and /media/images/ directory, the latter being where I want them to go.
The relavent settings are as follows:
# path relative to the settings.py file
DIRNAME = os.path.abspath(os.path.dirname(__file__).decode('utf-8'))
MEDIA_ROOT = os.path.join(DIRNAME, 'media')
MEDIA_URL = 'http://localhost:8000/'
ADMIN_MEDIA_PREFIX = '/media/'
I have tripple checked the local_settings.py file and there is no mention of the word 'media' anywhere in it, so I'm sure there are no setting overrides.
If it helps, I'm on Windows, but I'm using all the proper unix notation for my paths.
This is a Windows only bug. I am developing a Satchmo app on Windows and it does this, but when I deploy on a Linux box it works just fine. I just go into the database and edit the paths there when I am doing testing on my Windows box.
Turns out the issue is a problem with slash directions in the settings.py file.
Usually, I create a relative_path() function in my settings.py file so I can easily set:
MEDIA_ROOT = absolute_path('media')
The version of Satchmo I was using encouraged the use of a DIRNAME setting instead:
DIRNAME = os.path.abspath(os.path.dirname(__file__).decode('utf-8')
The issue was, using this technique, my MEDIA_ROOT was being set as such:
MEDIA_ROOT = os.path.join(DIRNAME, 'media')
But this was using the Windows backslashes instead of the Unix forward slashes. I've resolve it with:
MEDIA_ROOT = os.path.join(DIRNAME, 'media').replace('\\', '/')