I went to this route http://127.0.0.1:8000/static/corefiles/chatbot-data.xlsx but i get Page not found (404) error...
I added those to the URLs:
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
File directory is /rootproject/core/static/corefiles/testexel.xlsx
My settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = (
normpath(join(BASE_DIR, 'static')),
normpath(join(BASE_DIR, 'upload')), )
STATIC_ROOT = normpath(join(BASE_DIR, 'assets'))
Static files are to be used in your project, if you need to view a document at the specified url, you need to specify media url and media root in the settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Also add those to the URLs:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Related
This error occurs when performing command python manage.py collectstatic:
0 static files copied to '/home/project'.
That means there is no changes and my static files already exist in the destination but in this folder only one file:
static/
- staticfiles.json
But I want all my CSS, js, HTML for panel admin to be in it.
Django Settings:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = (
STATIC_ROOT,
)
urls.py
...
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I also tried these 1, 2 but no results were found.
settings.py
import os
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
urls.py
urlpatterns = [
path('demo/',Demo.as_view(),name='demo')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.STATIC_ROOT)
Add Your File in folder-
static->demo_folder->staticfiles.json
python3 manage.py collecstatic
Define all paths with STATICFILES_DIRS where Django will look for static files.
For example:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "templates/staticfiles")]
When you run collectstatic, Django will copy all found files in all your paths from STATICFILES_DIRS into STATIC_ROOT.
In your case, Django will copy all files to:
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
After that, your static files are accessible through your defined STATIC_URL URL. In your case:
STATIC_URL = '/static/'
I think I'm doing everything correctly but the images won't show on the website although they get added successfully in the media folder...
media/product_images
inside media is the placeholder image + product_images folder
Model Field
image = models.ImageField(upload_to='product_images/', default='placeholder.png')
Settings
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
Urls
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I've set things for the setting and the URLs as follow but it doesn't load the images from media directory:
settings.py:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media/')
urls.py:
urlpatterns = [
path('create/', views.image_create, name='create'),
path('detail/<int:id>/<slug:slug>/', views.image_detail, name='detail'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
this is the link generated for the page: https://127.0.0.1:8000/images/detail/1/django-and-duke/
and the link for the image: https://127.0.0.1:8000/media/images/2020/08/02/django-and-duke.jpg
I have another app named accounts and if I add the base URL of account to the first of this media URLs it works! but I know they are separated from each other.
if you need other parts of code please tell me.
Remove the slashes in os.path.join(BASE_DIR, '/media/')
If any argument to os.path.join begins with a slash / it will overwrite all previous arguments. You're currently setting MEDIA_ROOT to be /media/
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I would like to use dropbox as external storage. I have been working on this for a week and I think identified the problem.
url.py has:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Settings.py contains :
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
DEFAULT_FILE_STORAGE = "storages.backends.dropbox.DropBoxStorage"
DROPBOX_OAUTH2_TOKEN = #mytoken
I obtain the error:
ApiError('d5b8d0d14e950566defb5ca736ebda9a', GetTemporaryLinkError('path', LookupError('not_found', None)))
because it can't find the static there:
<img src="{% static 'logo.png' %}" height = 300></img>
How do I configure STATIC_ROOT and STATIC_URL (and MEDIA_ROOT/MEDIA_URL) in order to link to dropbox url? I'm expecting something like :
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
Thank you in advance.
if you have a model like:
class MyModel(models.Model)
photo = models.ImageField(upload_to="photos")
access the photo using MyModel.photo because it contains a URL that points to the photo in dropbox.
I'm deploying a Django application on heroku.
In my settings module, I have configured to host static files like
STATIC_ROOT = os.path.join(BASE_DIR, 'static_my_project')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_my_project')
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media_root')
and urls.py
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But on deployment to heroku, it gives error as
SystemCheckError: System check identified some issues:
ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
may be help this.
STATIC_URL = '/static/'
if not DEBUG:
STATIC_ROOT = ''
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
The problem is in the STATIC_ROOT and STATICFILES_DIRS there names cant be same.
static root is used for collectstatic command of django.
You can remove it if not using it.
And can also remove static files dirs if not changing its default position(inside django app) to somewhere else like base or something.