Static media fails to load in Django - python

My settings.py contains the following configuration parameters.
STATIC_ROOT = ''
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
'C:/Users/ABC/Desktop/DBMS/DjangoProject/tvshows',
)
My project's CSS file is located at C:/Users/ABC/Desktop/DBMS/DjangoProject/tvshows/static/default.css.
I have a mock HTML file that should pull in the CSS content, but the URL is a 404.
<link rel="stylesheet" href="{{ STATIC_URL }}static/default.css" />
What am I doing wrong?

Things to check:
DEBUG = True in settings.py
urls
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
use context processor or load static if {{ STATIC_URL }} isn't working
If {{ STATIC_URL }} isn't working in
your template, you're probably not
using RequestContext when rendering
the template.
As a brief refresher, context
processors add variables into the
contexts of every template. However,
context processors require that you
use RequestContext when rendering
templates. This happens automatically
if you're using a generic view, but in
views written by hand you'll need to
explicitally use RequestContext To see
how that works, and to read more
details, check out Subclassing
Context: RequestContext.

<link rel="stylesheet" href="{{ STATIC_URL }}default.css" />
You need to also edit your urls:

Related

Django CSS Not Loading

I'm having problems with my Django > css its not loading on the page for some reason am I missing something ?
I have attached snippets of my current code
Settings.py
urls.py
.html
put in you'r setting.py
INSTALLED_APPS = [
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = [
STATIC_DIR
]
then create folder static/file.css(nameyourfile.css)
and then put in your ...html :
<link rel="stylesheet" href= "{% static "file.css" %}">
{% load sataticfiles %}
if you use version 3. put :
{% load static %}
You need to do a few more things to configure your static files.
First, you need to make sure django.contrib.staticfiles is included in your INSTALLED_APPS in your django settings.
Also, you need to add this line in your html document:
{% load static %}
I would follow this guide which explains static files in the django docs.

Django not finding static js scripts in the correct folder

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

How to view images in django html template

I keep getting 404 error even though the url to the image is accurate. Yes, I have looked at official django docs and a whole lot of stackoverflow posts. I couldn't figure it out from those. If anyone can decipher what the instructions are saying to do then I will be grateful because they seem to be missing information.
index.html:
#I have tried static and staticfiles
{% load static %}
#a lot of code later...
<img border="0" src="{% static "logo.png" %}" alt="TMS" width="125" height="80">
Directory structure:
projectname
->__init__.py, settings.py, ...
manage.py
db.sql3
app_name
-> admin.py, models.py, views.py, templates->index.html, ...
static
->logo.png
The only thing about static that I have in settings.py along with 'django.contrib.staticfiles':
STATIC_URL = '/static/'
My views.py file has index function which returns HttpResponse(t.render(c)) where t is the index.html template and c is Context. Could those be the issue?
The url that results is this: http://127.0.0.1:8000/static/logo.png
You are using a static directory which is outside of your app (it is in the project folder), so in settings.py you need also:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

Django 1.6: static files

I am following the Django 1.6 tutorial on how to work with static files (https://docs.djangoproject.com/en/1.6/howto/static-files/). However, something is not working...
This is what I have so far...
URLS:
#mysite/urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^myapp/', include('myapp.urls')),
)
#mysite/myapp/urls.py:
from django.conf.urls import patterns, include, url
from myapp import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
VIEWS:
#mysite/myapp/views.py:
from django.shortcuts import render
def index(request):
return render(request, 'myapp/index.html')
TEMPLATES:
#mysite/myapp/templates/myapp/index.html:
{% load staticfiles %}
<img src="{% static "myapp/image1.jpg" %}" alt="Image 1"/>
In the mysite/settings.py file, I have STATIC_URL = '/static/'. In the folder mysite/myapp/static/myapp, I have a file named image1.jpg.
When I run the development server, it runs fine if I just put some other HTML in my index.html file, but when I try to display the image as above, it just shows a little white box where the image should be. Furthermore, if I go to localhost:8000/myapp/static/myapp/image1.jpg, I get a 404 error.
I'm assuming I'm doing something very obviously wrong...but what is it?
One thing - the STATIC_URL in settings.py is relative to the app directory, not the project directory, right?
Thank you!
When you're running in production, your static files are managed by the server (Apache, nginx) however when your DEBUG is set to true in your settings.py you'll have to tell django to serve the static files for you. To do this add the following to your URLs.py:
from django.conf import settings
(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),
This will allow static to be searchable and serve your static files.
img src="{% static 'myapp/image1.jpg' %}" alt="Image 1"/>

Problems linking to static files in Django 1.3

I'm running django 1.3, python 2.7 on windows XP
I'm trying to setup a css in a static folder for my django app.
The template looks like:
<html>
<head>
<title>css demo</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/my.css" />
</head>
<body>
The generated HTML looks like:
<html>
<head>
<title>css demo</title>
<link rel="stylesheet" type="text/css" href="http://localhost/static/css/my.css" />
</head>
...
So it appears that I have everything set up in order to specify where the static files are in the template, and then in the generated html.
But there is noting at 'http://localhost/static/css/my.css'. How do I get it there?
I ran collectstatic like so:
C:\root\workspace\mywebapp\src\mywebapp>c:\Python27\python.exe manage.py collectstatic
You have requested to collect static files at the destination location as specified in your settings file.
This will overwrite existing files.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Copying 'c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css'
1 static file copied to 'C:/root/workspace/mywebapp/src/mywebapp/static/'.
So I now have my.css in c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css
In my settings, I have:
STATIC_ROOT = 'C:/root/workspace/mywebapp/src/mywebapp/static/'
STATIC_URL = 'http://localhost/static/'
and in my url.py I have:
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
(r'^mywebapp/', include('mywebapp.css_demo.urls')),
)
urlpatterns += staticfiles_urlpatterns()
So if I understand correctly, my css at:
c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css
should no be available at:
http://localhost/static/css/my.css
But instead I see:
Page not found (404)
Request Method: GET
Request URL: http://localhost/static/css/my.css
Using the URLconf defined in mywebapp.urls, Django tried these URL patterns, in this order:
^mywebapp/
The current URL, static/css/my.css, didn't match any of these.
I also tried:
http://localhost/mywebapp/static/css/my.css
http://localhost/mywebapp/css_demo/static/css/my.css
Am I missing a step here?
The documentation on this is a bit confusing.
http://docs.djangoproject.com/en/1.3/howto/static-files/
http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/
Thanks!
I bet you have your app running at http://localhost:8000, not http://localhost :)
Change
STATIC_URL = 'http://localhost/static/'
to
STATIC_URL = '/static/'
No need for absolute URL there.
I created a folder:
C:\root\workspace\mysite\src\mysite\common
And in my settings, I have:
STATIC_ROOT = 'C:/root/workspace/mysite/src/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"C:/root/workspace/mysite/src/mysite/common/static",
)
This seems to work.

Categories

Resources