Static files not loading in django - django

I'm trying to create a homepage for my app but my page keeps loading after adding the STATIC_ROOT and STATICFILES_DIR. it gives Broken pipe from ('127.0.0.1', 49634) in my terminal. This is my home.html
This is my settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
this is my urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# -------- handle Static Files In dajngo --------
# Steps:
# 1 - Create (static) folder in Root Directory of Project
# 2 - Paste Your Static Files Folder in (static) Folder (js,css,images...etc)
# 3 - Now Do Setting in (setting.py)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
#-------- OR ----------
STATICFILES_DIRS = [BASE_DIR / 'static']
# 4 - add {% load static %} tag on top of html page
# 5 - Now use ({% static 'assets/image.jpg' %}) tag in HTML File for calling static files
# 6 - Done

Related

CSS files not loading in django

This is my first Django project. I cannot load CSS files onto my website. i've done everything possible.
Project Directries
firstproject/
assets/
static/
css/
<CSS files>
Settings.py/ static files
STATIC_URL = 'static/'
STATICFILE_DIRS = [
os.path.join(BASE_DIR, "static")
]
STATIC_ROOT = os.path.join(BASE_DIR,'assets')
urls.py
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
HTML file
<link rel="stylesheet" href="{% static 'css/bootstrap-grid.css' %}">
I think you made spelling mistake in staticfilesdirs in settings.py file.
Change this:
STATIC_URL = 'static/'
STATICFILE_DIRS = [ #Here you made spelling mistake. It should be `STATICFILES_DIRS`
os.path.join(BASE_DIR, "static")
]
To this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
And in link tag just add type="text/css".

Django 3.2.8: 0 static files copied to '/static'

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/'

Media files in Django

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 can't use static files from my Django app

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)

Why can't I find my static folder in my project

I'm working on a Django project and I can't see my static folder, I'm also having a problem displaying images.
And when I inspect the image div in the src it's written unknown, Here's how I display the image from the admin
``<img class="rounded-circle account-profile" src="{{ user.Profile.profile_photo.url }}" alt="profile">``
User is the current user, the profile and then the name of the column.
My folder structure is route-folder( project-folder, media-folder and app-folder )
My static settings
```STATIC_URL = '/static/'
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = [
os.path.join(SITE_ROOT, "static/"),
]```
The Url:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I think you are trying to retrieve a media image not a static one. I recomend using Whitenoise package to serve static files.
Follow instruction in this tutorial: http://whitenoise.evans.io/en/stable/django.html
Check you have all this setup:
# Settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
In the templates, you should remember to add the {% load static %} to grant access to the actual files.