I am working with the Django framework and am having some problems with referencing the source of an image from my HTML page. I continuously get this error in my terminal
Not Found: /bread.jpg
"GET /bread.jpg HTTP/1.1" 404 2445
although the last 4 digit string is different with every attempt I make to fix it.
My project (YeOldeShoppe) has an app (Shoppe) and is laid out like this
YeOldeShoppe
Shoppe
migrations
templates
index.html
urls.py
views.py
YeOldeShoppe
static
bread.jpg
settings.py
urls.py
manage.py
The code I'm having an issue with is in my index.html
{% load static %}
...
<div style="width: 25%; display: table-cell; text-align: center;">
<img src="{{STATIC_URL}}bread.jpg" alt="Great Value Bread"/>
<h2>Bread</h2>
<h5>$0.99</h5>
</div>
In my settings.py I've already set STATIC_URL
STATIC_URL = '/static/'
In my urls.py of Shoppe I tried to add it to urlpatterns
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
So far, I've tried to have the static folder be in the root directory of YeOldeShoppe
(eg. with manage.py), I've tried it in the YeOldeShoppe subdir (with settings.py) and in
my Shoppe subdir (with views.py). I tried having the both templates dir with the static dir
in the root (with manage.py), and I tried to use the command python manage.py collectstatic. I've also tried a variation of using "{{STATIC_URL}}bread.jpg" and "{% static "bread.jpg"%}".
index.html
{% load static %}
<img src="{% static "bread.jpg"%}" alt="Great Value Bread"/>
try adding these to the settings.py and urls.py:
settings.py
TEMPLATES = [
{
...
'DIRS': [...
(os.path.join(BASE_DIR, 'static')),
...
],
...
},
]
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Related
I tried several attempts through googling, but it didn't work. I don't think it was like this when I was working on another Jango project, but I don't know why.
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.HomeView.as_view(), name = 'home'), #to home.html
path("blog/", include('blog.urls')),
]
# urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "static/"
STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),)
# STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static")
home.html
<img src="{% static 'assets/img/navbar-logo.svg' %}" alt="..." />
directory
Project-folder
|
|__config
| |__settings.py
| |__urls.py
|
|__app
|
|__template
| |__app
| | |__bla.html
| |__home.html
|
| #bootstrap
|__static
|__assets
|__img
...
An Excerpt from the docs.
Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.
So, use list of dictionaries and also it is STATICFILES_DIRS not STATICFILES_DIR, you missed S.
Try this:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static"
]
Or this:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Make sure that you have loaded the static tag using % load static %.
If you've didn't loaded the static try this in your base.html or the home.html
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
I am trying to get static files to work in Django 3.2 (python 3.7) running in VS code (in Windows 11). My base template is referring to a style sheet at static/styles/style.css. The static folder is in the project root directory.
In my base template I have;
{% load static %}
<link rel="stylesheet" href="{% static 'styles/style.css'%}">
In my settings.py file, I tried both:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static"
]
And
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
I have also added the following to urls.py file:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
But I keep getting the following error:
"GET /static/styles/style.css HTTP/1.1" 404 179
Can someone please provide a solution?
I can't get access to the STATIC folder for my Django project.
Here's my project structure.
-myproject
--core
--settings.py
--myapp
--static
--------myapp
---- js
---- css
--templates
--static
In settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR, "static"),
]
print STATICFILES_DIRS:
['/media/my_pc/Apps/my_project/static']
In my main urls.py, I use this:
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Now, when I try to call the static .js file, it doesn't work.
error blook:
this Error on call base.html foces to the blook javaScript
in base.html
..
{%block javascript%}
{%endblock javascript%}
in my_use_htm.html
{%block javaScript%}
{%endblock javaScript%}
foces the name blook
I've already run collectstatic and created the static directory in my urls.py, and all other static files work fine (bootstrap, the custom CSS) and other scripts in the same directory work perfectly fine.
This is the error code in the console.
Failed to load resource: the server responded with a status of 404 (Not Found) ... jquery.formset.js:1
(http://localhost:8000/static/js/django-dynamic-formset/jquery.formset.js)
(index):86 Uncaught TypeError: $(...).formset is not a function
at (index):86
For the second error, I've run into a similar problem before. Moving the jQuery CDN tags above the script itself solved the issue then, but it's not working now.
This is my root urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('showroom/', include('showroom.urls')),
path('', RedirectView.as_view(url='/showroom/', permanent=True)), # Redirect homepage to showroom
path('accounts/', include('allauth.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
# Static and media files
STATIC_URL = '/static/'
STATIC_ROOT = (
os.path.join(BASE_DIR, 'static')
)
MEDIA_URL = '/media/'
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'media')
)
Django html
{% block scripts %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
{% load static %}
<script src="{% static '/js/django-dynamic-formset/jquery.formset.js' %}"></script>
{% endblock %}
This is the script section from the rendered html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/static/js/django-dynamic-formset/jquery.formset.js"></script> <script type="text/javascript">
$('.formset_row-image_set').formset({
addtext: 'add another',
deleteText: 'remove',
prefix: 'image_set',
});
</script> </body>
For context, this is an object creation form. The script allows dynamically created server-side formsets so users can upload individual images.
If there's any additional context necessary, just let me know.
Django handles static files in its own way you do not need to provide the full path to the location.
Try replacing with this line:
<script src="{% static 'js/django-dynamic-formset/jquery.formset.js' %}"></script>
also make sure you added on top of your file {% load static %}
As per documentation : https://docs.djangoproject.com/en/2.2/howto/static-files/#configuring-static-files
I love Django but getting static files served in development is painfully flakey. I have installed webassets to make the job easier. Basically, my assets are 404'ing with my current configuration but admin assets are fine (?!). My project layout is like this;
myapp/
common/
static/
bootstrap/
img/
less/
js/
templates/
__init__.py
assets.py
urls.py
models.py
views.py
projects/
templates/
static/
__init__.py
assets.py
urls.py
models.py
views.py
public/ <- (static files collected here)
settings.py
In my settings I have the following values configured
__DIR__ = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(__DIR__, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(__DIR__, 'public')
STATIC_URL = '/static/'
My urls are configured like this;
urlpatterns = patterns('',
url(r'^projects/', include('myapp.projects.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('myapp.common.urls')),
)
urlpatterns += staticfiles_urlpatterns()
Again, assets.py looks pretty standard;
from django_assets import register, Bundle
js = Bundle(
'bootstrap/js/bootstrap-alert.js',
'bootstrap/js/bootstrap-button.js',
'bootstrap/js/bootstrap-carousel.js',
'bootstrap/js/bootstrap-collapse.js',
'bootstrap/js/bootstrap-modal.js',
'bootstrap/js/bootstrap-popover.js',
'bootstrap/js/bootstrap-scrollspy.js',
'bootstrap/js/bootstrap-tab.js',
'bootstrap/js/bootstrap-tooltip.js',
'bootstrap/js/bootstrap-transition.js',
'bootstrap/js/bootstrap-typeahead.js',
output='bootstrap/script.js',
debug=False
)
register('js', js)
And my base template is very basic;
{% load assets %}
{% assets 'js' %}
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}
So, when runserver is running, all the js files are bundled into 1 tag with the following url http://localhost:8000/static/bootstrap/script.js?ed501ad2. But this 404's with the message "'bootstrap/script.js' could not be found".
If however, I log into the /admin app then all css assets are rendered correctly. I've run collectstatic and verified that the assets does actually live within public/ directory.
What am I missing here?
Did you add django_assets.finders.AssetsFinder to your STATICFILES_FINDERS as is required in the document of django-assets?
http://elsdoerfer.name/docs/webassets/django/index.html