I am deploying my Django site on A2 Hosting.
I can get the page to display but no static files (images/css) are loading.
I have this in my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
ROOT_PATH = os.path.dirname(__file__)
STATICFILES_DIRS = [os.path.join(ROOT_PATH, 'static')]
my urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('mysite.urls')),
url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'), name='home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I am running python manage.py collectstatic in the terminal. It is creating a static folder with a file structure including admin and subfolders within that admin folder. My images and css files are appearing in the newly generated static file (in the base folder) yet neither the css nor my images are showing on my webpage (I've been restarting my server).
My link to my css file within my html template is:
<link rel="stylesheet" href="{% static 'style.css' %}" type="text/css">
This is done on shared hosting, I have debug set to false, and it works in development.
/home/mysite
->etc
->mysite
-->__pycache__
-->webpage
--->static
---->webpage
----->image.jpg
----->style.css
--->templates
---->webpage
----->index.html
-->main_webpage
--->settings.py
-->public
-->static_files (generated by collectstatic)
--->admin
--->image1
--->style.css
-->template
-->tmp
->logs
...
Thank you.
Related
Here is the configuration of my static_root variable:
STATIC_ROOT = [os.path.join(BASE_DIR,'vol/web/static')]
My templates are at the root of the project.
TEMPLATES={
...
'DIRS':[os.path.join(BASE_DIR, 'templates')],
...
}
This is how I call my bootstrap.min.css file in a project template:
<link rel="stylesheet" href="{% static 'assets/plugins/bootstrap/css/bootstrap.min.css' %}">
But it doesn't work because the file is not loaded. What's the right way to do it?
Inside main urls.py
from django.conf import settings as SETTINGS
from django.conf.urls.static import static
urlpatterns += static(SETTINGS.STATIC_URL, document_root=SETTINGS.STATIC_ROOT)
urlpatterns += static(SETTINGS.MEDIA_URL, document_root=SETTINGS.MEDIA_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
STATIC_URL = '/static/'
STATICFILES_DIRS = (BASE_DIR / 'static',)
STATIC_ROOT = (BASE_DIR / 'static'/ 'static')
If you store your js and css files in some folder, then you need to specify this folder in STATICFILES_DIRS django setting. For example:
STATICFILES_DIRS = [
"/home/my-user/my-project/static",
]
Then Django will search this directory(ies) when looking up files specified in {% static ... %} tag.
https://docs.djangoproject.com/en/4.0/ref/settings/#staticfiles-dirs
If Django still unable to find particular file, try using findstatic management command for diagnostics. This will show in which directories django is actually looking for the file. For example:
python manage.py findstatic -v3 assets/plugins/bootstrap/css/bootstrap.min.css
https://docs.djangoproject.com/en/4.0/ref/contrib/staticfiles/#findstatic
I am trying serve static files in my local development (on same server I serve my site). I have run ./manage.py collectstatic with the following configs:
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
template.html
{% load static %}
<img src="{% static 'apis/icons/random-svgrepo-com.svg' %}" style="width: 50px;">
Now I have staticfiles folder in my BASE_DIR folder, but when I try to get an image, it doesn't appear to be working expected.
http://127.0.0.1:8000/static/apis/icons/random-svgrepo-com.svg
Please help. What I am doing wrong?
First of all I think you have not configured your static files and media files. Try configuring it as follows. In your settings.py ensure to include STATICFILES_DIR, STATIC_ROOT, MEDIA_URL, MEDIA_ROOT in your settings.py and then add the below lines below STATIC_URL = 'static/':
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
STATICFILES_DIRS = [BASE_DIR / 'staticfiles']
STATIC_ROOT = BASE_DIR / 'staticfiles/'
MEDIA_ROOT = BASE_DIR / 'static/media'
By doing this you are telling django where to get your static files. now your have to add the link of the static files in your project urls.py. you will add that as below.
from django.conf import settings
from django.conf.urls.static import
static
urlpatterns = [
.......
]
urlpatterns +=static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
One last thing that I assumed you had done is creating static folder in your project root and inside that static folder create media folder where all the images you want to load are. Now run python manage.py collectstatic
I'm starting to configure my first Django project and I find this issue which is really bothering me.
I have set a root static folder to put some css files for my base template there, but Django is not finding any css files there.
My settings.py are like this:
...
BASE_DIR = Path(__file__).resolve().parent.parent
...
STATIC_URL = '/static/'
SATICFILES_DIRS = [
BASE_DIR / 'static',
BASE_DIR / 'sales' / 'static'
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
...
...
And in my urls.py I have:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('sales.urls', namespace='sales')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT, show_indexes=True)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT, show_indexes=True)
...
If a run findstatic I get the following:
$ python manage.py findstatic --verbosity 2 static
No matching file found for 'static'.
Looking in the following locations:
/home/dancab/git/django-3-course/myenv/lib/python3.9/site-packages/django/contrib/admin/static
/home/dancab/git/django-3-course/src/sales/static
And also, in the browser, I can see the list of files in the MEDIA folder, but I can't see the STATIC folder, I get the following error:
I don't understand why I Django finds the MEDIA folder and not the STATIC folder.
Thanks in advance to anyone that gives me a hint on why this happens.
before the line STATIC_URL = '/static/', set static root like
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'project_folder/static') # if not works, set actual path
]
don't forget to place your project folder name.
Then run
python manage.py collectstatic
Then you can load static using jinja expression in your html files like
{% load static %}
Then you can link your static css files in root->static->css folder like
<link rel="stylesheet" href="{% static 'css/your.css' %}">
I am using Django with a Bootstrap template, that requires Jquery. But I am having trouble with a js file.
I created static directory, and static_cdn directory.
I am using a Bootstrap 4 template.
My project template requires a js file (template doesn't working correctly without this js file) but this js file is not using a valid url; It is calling all my svg files, but with a nonvalid URL.
This is my project static files folder :
This is my urls.py urls :
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^panel/$', panelView, name='panel'),
url(r'^pageOne/$', pageOne, name='pageOne'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(os.path.dirname(BASE_DIR), "static"),
# '/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
MEDIA_URL = '/evrak/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "evrak")
This is my html page :
<!-- BEGIN: Vendor JS-->
<script src="{% static 'app-assets/vendors/js/vendors.min.js' %}"></script>
<script src="{% static 'app-assets/fonts/LivIconsEvo/js/LivIconsEvo.tools.js' %}"></script>
<script src="{% static 'app-assets/fonts/LivIconsEvo/js/LivIconsEvo.defaults.js' %}"></script>
<script src="{% static 'app-assets/fonts/LivIconsEvo/js/LivIconsEvo.min.js' %}"></script>
{% block vendorJS %}
{% endblock vendorJS %}
<!-- END Vendor JS-->
<!-- BEGIN: Page Vendor JS-->
{% block pageVendorJS %}
{% endblock pageVendorJS %}
<!-- END: Page Vendor JS-->
Now,
Django is able to load everything in static directory. And also everything is working correctly.
But, a js file is calling my all svg files , and it is using a nonvalid url.
This is my problematic js file :
"{% static 'app-assets/vendors/js/vendors.min.js' %}"
This is my errors :
You can see 'initiator' field that shows who is calling files.
So, all 'red' files' initiator is 'vendor.min.js'.
If you look, problem is all about this 'vendors.min.js' file. Django is loading everything correctly first, but after loading this js file, it is calling all svg files with a nonvalid url; so browser throws 404 not found error.
Normally everything is correct like that :
Django is loading everything correctly , but this vendors.min.js thing is calling all files in a wrong way, so Django can not find them.
I couldnt find how to change this js file to call a valid address, or add 'app-assets' url directory to Django.
How can I fix this problem ?
I think , if I can add a static url path starts with '/app-assets/' directly , all will work . But for now couldn't find, how to add a second static url path.
Solved.
Added a new path that uses directory, and solved.
This is new settings :
STATIC_URL = '/static/'
STATIC_URL2 = '/app-assets/'
STATICFILES_DIRS = [
os.path.join(os.path.dirname(BASE_DIR), "static"),
# '/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
STATIC_ROOT2 = os.path.join(os.path.dirname(BASE_DIR), "static_cdn/app-assets")
MEDIA_URL = '/evrak/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "evrak")
This is new urls.py :
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^panel/$', panelView, name='panel'),
url(r'^pageOne/$', pageOne, name='pageOne'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.STATIC_URL2, document_root=settings.STATIC_ROOT2)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now, everything is working.
both
'http://127.0.0.1:8000/app-assets/fonts/LivIconsEvo/svg/share.svg'
and
'http://127.0.0.1:8000/static/app-assets/fonts/LivIconsEvo/svg/share.svg'
links are valid now.
I have troubles with upgrading my project from Django 1.8 to Django 1.10: static files are not loaded anymore.
My template looks like this:
{% load staticfiles %}
<!DOCTYPE html>
...
<link href="{%static 'file.css' %}" rel="stylesheet">
...
Then in my file settings.py, I have 'django.contrib.staticfiles' as an installed app. DEBUG is set to True, and I have:
STATIC_URL = os.path.join(BASE_DIR, 'static/')
STATIC_ROOT= os.path.join(BASE_DIR,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), "./", ]
But when the html is produced, it is like the %static has no effect anymore. It is replaced by the empty string (the same works fine with Django 1.8, where the %static is replaced by the content of STATIC_URL). Does anyone know how to fix this ?
Can you added the Update the urls.py(mainproject/urls.py), once you made the chnages run the python manage.py collectstatic command.
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
===================
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
def root(folder):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',folder)
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
root('static'),
)