Django Static Url Error, Not Loading - python

I'm currently mad at Django (1.9) right now! The saddest thing is 'Static URL' is the one giving me problem. 'Media URL' is working fine, no problem, but the static url is giving a huge headache.
in my settings_dev.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH,'../static/')
STATIC_URL = '/static/'
when I add the below tag:
{% load static from staticfiles %}
<script type="text/javascript" src="{% static 'datepicker/js/bootstrap-datepicker.js' %}"></script>
The js file won't load. when I check my source code, it will display the below link.
<script type="text/javascript" src="/static/datepicker/js/bootstrap-datepicker.js"></script>
And when I click it will redirect me to
http://127.0.0.1:8000/static/datepicker/js/bootstrap-datepicker.js
And display
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/datepicker/js/bootstrap- datepicker.js
Now, I adjusted my urls.py to
if settings_dev.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings_dev.MEDIA_ROOT, 'show_indexes': True}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings_dev.STATIC_ROOT, 'show_indexes': True}),
)
Yet, I'm still getting the same error!! Page not found issues.
Project Directory
PROJECT NAME: Book/
SUB DIRECTORY:
media
static
Template
book
bookapp
manage.py (this is a file)
What am I missing?

Okay to make things clear for you.
STATIC_ROOT is that directory where all your static data gets collected when you want to serve your files on another server, say APACHE or NGINX or maybe on Heroku or so.
If don't want just want to run your web-app on your local development server, you don't require python manage.py collectstatic and hence you don't need STATIC_ROOT.
All you need is STATIC_URL and in case you have your static files at some other location as well then you also need STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),].
So you'll be having a folder named `static' at the base directory level where django will look for your static files.
If you specify the same folder for STATIC_DIRS and STATIC_ROOT then you'll automatically get an error. Django won't allow you to do that as technically you are trying to give the same directory for two different purposes.
See this for a detailed explanation -> Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]
That line is enough for serving your project static folder files... And you have to set this in your urls.py
urlpatterns = [
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Related

Get 404 1668 in CMD and can't connect my css file im html

Here's my script in base.html, the left of the picture is about my tree.
I checked the href should be correct and I've load static at very top of the html file. However I still cannot connect the css file anyone can help he here? Thanks!
href="{% static 'Simple_Social/css/master.css' %}"
for refer, please see the picture since idk why I can't post a picture yet, thanks!
https://i.stack.imgur.com/Vi2z6.png
This is how static files are configured. In settings.py file inside the INSTALLED_APPS list, there is an app called 'django.contrib.staticfiles', this baked in app manages the static files across the entire project during development as well in production.
This static URL will append to base URL for serving static files in development like http://127.0.0.1:8000/static/css/style.css basically think this as a reference to static files.
STATIC_URL = '/static/'
STATICFILES_DIRS tells Django the location of static files in our project. The common practice is to have all the static files in a top-level static directory.
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
STATIC_ROOT is the single root directory from where the Django application will serve the static files in production. The command manage.py collectstatic will automatically compile all the static files throughout the project and dump it into a single root directory, which is declared in STATIC_ROOT.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Finally, you can user load static to load the static files in template
{% load static %}

Django: my css static path does not exist

My link looks like this:
<link rel="stylesheet" type="text/css" href="{% static 'css/background.css' %}">
This is fine because it gets the path I want which is href="/static/css/background.css". I use {% load static}. I have django.contrib.staticfiles in my INSTALLED_APPS. This is some more relevant info on my settings.py:
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static"),
]
But I'm pretty sure this is right too. My problem is that static/css/background.css cannot be found by my local server. I know this because http://localhost:8000/static/css/background.css gives me a 404 error. I don't know why this is the case because this is my project file path:
Project
Project
src
static
css
background.css
Have you set your url paths ? Here is a snippet from the official Django documentation.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I fixed my problem and I'm posting this in case anyone this can help anyone else. My problem is that I had my static folder outside my app. It turns out that my website is targeting my app and so when django was looking for the static folder it was looking for it in my app. So try putting your static files in your app and see if that helps.

Django static image not displaying

I have looked every where possible, I have not found the answer to the issue I'm having. I spent hours in this and find not avail.
My front end was displaying all images and css with a static folder under the project or under the app tree.
After I did the changes to use a dynamic url, and created the static folder out of the project folder(one folder down) did collectstatic all of sudden my images have disappeared, css still works either direct path'd or dynamically.
I have tried the same with images, direct path(as a copy of the folder still remains in the project folder) the direct path does not work, neither the dynamic path.
Its not returning 404 either, on the first load, it returns 200, on second 304 as if the image was there on screen, but it isnt. There is just no errors, but darn image is not there.
What puzzles me is that the css works, but image does not display.
Bellow is my code.
---|settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),
#'/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
---|url.py
if settings.DEBUG == True:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
---|main.html (template)
{% load staticfiles %}
<img src={% static "images/banner_top.png" %}>
or
<img src="..static/images/banner_top.png">
or
<img src="static/images/banner_top.png">
on browser the image doesn't show, but back end shows 200 and right path to image.
http://127.0.0.1:8000/static/images/banner_top.png
[22/Oct/2016 22:32:10] "GET /static/images/banner_top.png HTTP/1.1" 200 0
Here is the HEADER
GENERAL
Request URL:http://127.0.0.1:8000/static/images/banner_top.png
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8000
REQUEST
Content-Length:0
Content-Type:image/png
Date:Sun, 23 Oct 2016 08:06:17 GMT
Last-Modified:Fri, 21 Oct 2016 06:54:06 GMT
Server:WSGIServer/0.1 Python/2.7.10
Well, problem has been solved. Koterpillar over #django IRC help me on figuring this out.
Somehow the system emptied the images files(corrupted) (which I think happened over collectstatic) and that's why the images were loaded and nothing displayed.
upon checking
curl -D - http://127.0.0.1:8000/static/images/banner_top.png
which only returned the header and no content. So, following on check the images files and verifying that the contents where gone. I replaced with new images and then it was displayed again on website reload.
Many thanks to Koterpillar
first I don't understand why you set like below.
urls.py
if settings.DEBUG == True:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),
#'/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
because you write static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) in urls.py and at the same time, your are using django.contrib.staticfiles app. If you put django.contrib.staticfiles in INSTALLED_APPS, It sever static files automatically. you can use static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) in urls.py when you want to sever static files manually.
please check serving static files during development
one option
1.you don't use django.contrib.staticfiles.
INSTALLED_APPS = [
(...)
# 'django.contrib.staticfiles',
]
then serve static files maunally.
in urls.py
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
in settings.py
STATIC_URL = '/static_cdn/'
STATIC_ROOT = os.path.join(BASE_DIR, "static_cdn")
if you collectstatic under project folder
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
second option
1.you use django.contrib.staticfiles.
INSTALLED_APPS = [
(...)
'django.contrib.staticfiles',
]
STATIC_URL = '/static_cdn/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static_cdn"),
and other path
]
first, it will find static files under app folder tree(now I set STATIC_URL = '/static_cdn/', so in folder which has name as static_cdn). if you want to get static files not in app folder tree, you set STATICFILES_DIRS =
I don't know why it return 200 and doesn't show any image on your browser exactly. but I assume that you mixed two together and somehow it caused some problem. please check it.
I hope my idea is helpful for you! Good luck!
I went through the same problem. I was using Django 1.11
So the solution I found after several tweaks:-
This works: Without using {% load staticfiles %}
{% static '/images/banner_top.jpg' %}
Press Ctrl+Shift+R . Press these 3 keys together

How do I display an image on my webpage in Django?

I want to display an image on my website. I've been looking through the Django documentation and the other posts on stackoverflow, but I haven't gotten this to work.
I have an image name 'under-construction.jpg'. It lives in the /home/di/mysite/myapp/static/images directory.
I have a template like this:
<img src="{{ STATIC_URL }}images/under_construction.jpg" alt="Hi!" />
in my views.py I have this:
def under_construction(request):
return render(request, 'under_construction.html')
In my settings.py, I have this:
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/home/di/mysite/myapp/static',
'/home/di/mysite/myapp/static/images',
)
I executed ./manage.py collectstatic and it put a lot of files in /home/di/mysite/admin and /home/di/mysite/images. What do I have to do get my image to show up?
All you need to do is edit settings.py to be as the 4 following points and create a new folder (static) in the same folder settings.py is located
in the folder static you can create another folder (images) in it you can put the (under_constructioon.jpg)
STATICFILES_DIRS = (os.path.join( os.path.dirname( __file__ ), 'static' ),)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',)
STATIC_URL = '/static/'
STATIC_ROOT = ''
after you're done with the prev. points you can write
<img src="{{ STATIC_URL }}images/under_construction.jpg" alt="Hi!" />
I have faced same problem displaying the static image. suffered a lot and spent a lot lot of time in this regard. So thought to share my settings.
settings.py
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
import os.path
STATICFILES_DIRS = (
"D:/temp/workspace/offeronline/media",
(os.path.join( os.path.dirname( __file__ ), 'static' )),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ... and at the end of the file add the following line
urlpatterns += staticfiles_urlpatterns()
and finally added the following code to the template tag and it worked
<img src="{{ STATIC_URL }}images/i1.png" />
I have solved that problem like this.
STATIC_ROOT = '/path/to/project/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
)
Django will not directly get files directly even you do this. You need to link this static directory for each application of yours. Just go to the django app dir run following command..
cd /path/to/project/my_proj/my_app
ln -s /path/to/project/static/
This will work only in debug mod. You need to set DEBUG = true in settings.py file. This should work with django's development server.
Django won't serve any static file in production mod. You need to serve static files with web-server in production mod.
More information can be found here..
Django does support static files during development, You can use the django.views.static.serve() method in view to serve media files.
But using this method is inefficient and insecure for production setting.
for Production setting in Apache
https://docs.djangoproject.com/en/1.2/howto/deployment/modpython/#serving-media-files
set STATIC_ROOT = /path/to/copy/files/to
have you added
urlpatterns += patterns('django.contrib.staticfiles.views', url(r'^static/(?P<path>.*)$', 'serve'),
or you can also do this
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ...
urlpatterns += staticfiles_urlpatterns()
and of course try not to server static files through django it does have some overhead instead configure your http server to serve them, assuming you have an http server (nginx is quite good).

Django static files problem

Duplicate of Django staticfiles app help
I'm using Django 1.3 beta and the static files app is confusing. In development mode it is meant to automatically serve files from the STATIC_URL path.
From http://docs.djangoproject.com/en/dev/howto/static-files/
If you're using the built-in
development server (the runserver
management command) and have the DEBUG
setting set to True, your staticfiles
will automatically be served from
STATIC_URL in development.
This didn't seem to work, so I tried a url pattern ('/static/') which routes to the static.serve view. This just 404'd. Somehow it conflicts with the STATIC_URL, if I change it to 'assets/' it will serve the files from static just fine. It's only logical to use '/static' for the static url, but this conflicts.
Url Patterns:
urlpatterns = patterns('',
# Serve static files for *development only*
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
Static files settings:
STATIC_ROOT = '/home/dave/static/flux'
# URL that handles the static files served from STATIC_ROOT.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
Ideally I would like Django to use the static url for seving files in development without having to use any urlpatterns.
If you want to serve up the static files while using the built in Django server you will need to add a urlpattern. This is what I do (add this after all your other patterns:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_PATH}),
)

Categories

Resources