When I add a random picture inside an HTML it doesn't load at all.
Here is how I set up my Django Project:
Inside of index.html:
{% load static %}
<img src='{% static "img/logo2.png" %}'>
inside of Production, base and local.py I have this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
Thank you in advance if you can help me.
Basic Configuration
STATIC_URL = '/static/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
In your server try to access http://localhost:8000/static/img/logo2.png and check if the image is propely loaded, if the image loads so your confugiration at settings.py is right.
EDIT: You using heroku? im not sure if you will e able to use your static files with django at heroku, probabily use some external static server as Amazon S3
Here static folder was inside myapp but you have given root
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
place the static folder into settings of blankapp
change the img src in HTML
check now
Related
I have a problem with loading static files. It wont load images on my page at all.
When i look for the address it says /apps/assets/img..... But it should be /static/assets/img like on the home page and login page. (/apps/ is my view name but i don't get it why it loads it like that..(
All apps are in my installed apps directory,
DIRS in templates is configured,
i have 'django.contrib.staticfiles',
tried running collectstatic,
rerunning the server,
ALLOWED_HOSTS = ["*"] ,
STATIC_URL = '/static/' ,
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root'
Steps to handle static files in Django:
Create a (static) folder in the Root Directory of the Project
Paste Your Static Files Folder in (static(js,CSS,images...etc)
Now Do Setting in
setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
add {% load static %} tag on top of html page
Now use {% static 'assets/image.jpg' %} tag in HTML File for calling static files
Done
Set DEBUG value to True in settings.py. Remember that DEBUG value should be almost ALWAYS set to False in production, so read about overriding settings via additional file (settings_local.py and et cetera).
Currently I am getting my static files like this:
src="{% static 'website/images/home-slider/plantarte-espacio-4.jpg'%}"
And my settings.py look like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'website/static'),
)
Now wat I want is that instead of grabbing the static files from the static foleder inside my app. It goes to my AWS S3 bucket I created and uploaded all the files to
Instead of this:
src="/static/website/images/home-slider/plantarte-espacio-4.jpg"
Do this:
src="https://plantarte-assets.s3.us-east-2.amazonaws.com/website/images/home-slider/plantarte-espacio-4.jpg"
If someone could please help me I would really apreciate it.
In the simple case, change your
STATIC_URL = '/static/'
to
STATIC_URL = 'https://plantarte-assets.s3.us-east-2.amazonaws.com/'
and you should be golden.
setting before any change.
STATIC_URL = '/static/'
here I want to create one static files folder rather than assigning them to each every app by creating there name as directory and then I have to assign. So I want one common folder for all JS and Css which can be reflected in each and every template rather than defining all of them at different places.
firstly make below changes in your settings.py file :
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "allfiles"),
]
Remember that allfiles is also folder in your project root.
After that in your template load static files as given below:
inside template.py:
{% load static %}
<html><script src="{% static 'index.js' %}"></html>
<--this file is in your common file you can replace it with any JS or CSS-->
This is quite strange. It was working OK, but the machine it was running broke and I had to mount it again without success. The webapp shows with wuite background, without images, css nor js loaded.
This is how the app is structured:
These are the variables in settings.py which matter (please ask for any other if required):
DEBUG = True
TEMPLATE_DEBUG = DEBUG
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/var/www/myapp/static/',
)
And into my tempaltes, I always set the contents' URL like this:
<img id="logo" src="{{STATIC_URL}}images/logo.png"\>
Any idea about where the problem is?
The settings you have are generally discouraged, for local development they will work fine, however if you were to attempt to run that on a Linux based machine, it wouldn't work.
Here's how it should be formatted:
Here are the settings that would make it like that:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
#this applies to all operating systems
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'static_dirs'),
)
You would put your static folders and files in "static_dirs", and then whenever you needed to sync to "static_root". You would type: "python manage.py collectstatic".
In your template file you would then type:
{% load static from staticfiles %}
<img id="logo" src="{% static "images/logo.png" %}"\>
That should work.
check if the source is loaded using view source and if yes from there check if any link to your static assets are accessible if present then you have to hard refresh the browser
Include {% load staticfiles %} in template.
You comment in reply to the post by Allen Fernandes above indicates you are in DEBUG mode. Is django.contrib.staticfiles in your INSTALLED_APPS?
I finally discovered the error: Apache was running as default and serving the html templates in port 80, but without permissions for the static files.
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).