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.
Related
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 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.
I am pretty new to Django and I do not know how to link CSS and or javascript to my project.
Don't want to have a project with no CSS. Thank you for any help, sorry if this is a very dumb question. Have a nice day!
you problem is that you need to link static files from your html template.
this is fairly easy in django, just create a folder called staticfiles in the root directory and create a folder called css inside of it. Put your styles in there.
go to your settings.py and change
STATIC_URL = whatever
STATIC_ROOT = whatever
to
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIR = [
os.path.join(BASE_DIR, "staticfiles")
]
you see, in django settings, you can't have the static root be the same as what folder you are using for static files in production.
for your urls.py in the folder inside the base directory named after your project
add this after urlpatterns
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += (static(settings.STATIC_URL, document_root= os.path.join(settings.BASE_DIR, "staticfiles")))
now in your base template
put
{% load static %}
before the html tag and
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
in the head tag. I hope this has helped!
I am trying to have an image (logo) on the navbar.
My project is called "mysite-project" (where manage-pyis), it contains the app "mysite".
In order to upload my static file I did the following:
1) mysite-project/mysite/settings.py
I added:
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/')
]
2) Created folders static and added my logo.png in:
mysite-project/static/mysite-project/logo.png
3) mysite-project/templates/base.html
{% load staticfiles %}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'home' %}">
<img src="{% static 'mysite/logo.png' %}" height=30 width=30 class="d-inline-block alighn-top" />
Code of Conduct
</a>
</nav>
4) In mysite-project/mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/', include('django.contrib.auth.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
HOWEVER the image does not show up. I think I have some issues in the settings.py for the folders but I cannot find where
The issue is in your STATICFILES_DIRS setting. If you join a path that has a leading slash then the result will "ignore" any preceding arguments and everything after will be relative to root
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/') # This will result in "/static/"
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static') # This will result in "<BASE_DIR>/static/"
]
STATIC_ROOT is the directory that you want to serve files from and the directory that collect_static will populate with all your static files, STATICFILES_DIRS is where Django will collect the files from. STATICFILES_DIRS shouldn't contain STATIC_ROOT. The usual layout for a project is something like this
myproject/ # The root of your repo
myproject/
myapp/
static/ # This is where you put app specific assets
...
static/ # This is where you put your generic static assets. Add this to STATICFILES_DIRS
...
static/ # This is STATIC_ROOT and where your files are served from after being collected
The default value for STATICFILES_FINDERS will look in STATICFILES_DIRS and every apps static directory. If you are using git, you should add the static folder at the root of your repo to .gitignore
I have a template that renders an image:
{% load staticfiles %}
<img src="{% static "img/logo.png" %}" alt="My image"/>
The image link is broken, but it points to:
localhost/static/img/logo.png
What are values I need to set for static_root, static_url, and STATICFILES_DIRS to get this image to show up correctly?
This is my directory structure:
myprojectname (top level)
--- myprojectname
--- --- myproectname
--- --- --- settings
--- --- --- --- base.py (setting.py)
--- --- static
--- --- --- img
This is my static configuration in settings:
STATIC_ROOT = '/Users/myuser/myprojectname/myprojectname'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
#normpath(join(SITE_ROOT, 'static')),
os.path.join(BASE_DIR, "static"),
'/Users/myuser/myprojectname/myprojectname/static',
)
This is what it shows:
I have already done a collectstatic and this doesn't work.
Static files can be confusing in Django. I'll try to explain as simply as possible...
STATIC_ROOT
This is the directory that you should serve static files from in production.
STATICFILES_DIRS
This is the directory that you should serve static files from in development.
STATIC_ROOT and STATICFILES_DIRS cannot point to the same directory.
Django is a highly modular framework. Some application modules contain their own templates, css, images and JavaScript. Django admin is one such app. Django extends this modularity to applications you create by using different directories for static files in development versus production.
When DEBUG = True and you have included django.core.staticfiles in your INSTALLED_APPS, Django will serve the files located in the STATICFILES_DIRS tuple using the STATIC_URL path as the starting point.
In production this responsibility should be given to Nginx, Apache, CloudFront, etc. When DEBUG = False, Django will not automatically serve any static files.
When you run:
$ python manage.py collectstatic
The files specified in the STATICFILES_DIRS are copied into the STATIC_ROOT to be deployed.
So, to answer your question, I would do the following:
Create another directory to store your static files in development and add that path to your STATICFILES_DIRS. I usually call this folder "static-assets". It can reside at the same level as your existing "static" directory.
Set STATIC_ROOT to the path to your existing "static" directory.
If you look closely at the path that's returning a 404 in your screenshot, the image path is specified as: /static/img/logo.png, but your directory for images is: /static/image/
So double-check your image path to make sure you're pointing to the right directory.
Make folder "staticfiles" in root of your project where settings.py exists
In staticfiles you can go this way..
css
js
images
In settings.py
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'staticfiles'), # if your static files folder is named "staticfiles"
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR,'template'), # if your static files folder is named "template"
)
In base.html
<link rel="stylesheet" type="text/css" href="{% static 'css/demo.css' %}" />
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
In other template files in which you include base.html
{% extends "base.html" %}
{% load static %}
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<div id="yourID" class="yourClass">
<img src="{% static "images/something.gif" %}" alt="something" >
</div>
If you are in development mode then define just one line:
STATICFILES_DIRS = ( os.path.join('static'), )
You don't need to define STATIC_ROOT = '/Users/myuser/myprojectname/myprojectname'
OR
STATICFILES_DIRS as BASE_DIR
So, working solution is :
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )
actually I too was struggling with the same problem , it got resolved now , check javapoint documentation once its really good , another method that worked for me is change the path of static folder i.e place the static folder inside the app folder that u have created (in my case , the static folder is inside the first_app folder) . it should work . after that I relocated my folders back to where it was and to my surprise , it worked again!. sometimes it may be the fault of browser . I tried my level best to explain as I am a beginner in Django .
If you want to save static files in Django you will need to do the following
in settings.py file you will have something like this `
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'my_project/static') # where I want to host the static files i.e img, js & css
]
depends on how you have structure you files but you will need to have something like this in your urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('', include('appfront.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Final your template will have something like this Note the usage of static tag
{% load static %}
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
<div class="container">
<a class="navbar-brand" href="{% url 'index' %}">
<img src="{% static 'img/logo.png' %}" class="logo" alt="">
</a>
'''