Static Images not Loading on Django Project - python

I just had to restart my app from scratch (at least from setup point of view) due to a migration issue which seemed to mess up my whole db.
Anyway, my problem now is with my img and css directories not loading at all.
I've verified the urls.py is working fine, as I can access urls specified in there, but no images, css, etc. are showing up.
In settings.py I have:
STATIC_ROOT = '/Users/c0de/appWs/git/djangoapp/MyApp/MyApp/Scanner/static/'
PROJECT_ROOT_LOCAL = "/Users/c0de/MyAppWs/git/djangoapp/MyApp/MyApp/"
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT_LOCAL, 'Scanner/templates/'),
)
INSTALLED_APPS = (
#django-default apps
'MyApp',
)
In my templates, I'm doing calls like:
<img src="{% static "img/templated/home/img.png" %}" width="85" height="85" alt="img" />
templates and static dir are at this path:
/Users/c0de/appWs/git/djangoapp/MyApp/MyApp/Scanner/templates/
/Users/c0de/appWs/git/djangoapp/MyApp/MyApp/Scanner/static/
I've also tried restarting my local default-django server.
Any ideas? Thank you!

Silly, silly me.
All this time I thought it was an issue with the paths of STATIC_ROOT or TEMPLATE_DIRS or STATIC_URL
It came down to loading not the application, but the project in INSTALLED_APPS. It still seems strange it would show the whole application, but not the img or css files, but still lload the template.
Now that the actual application is set in INSTALLED_APPS() and not the project (containing this application) this seems to be working fine.

Related

Django, keep both global and local STATIC folders

I'm quite new in Django, coming from PHP and node mostly.
By default, I understand that every app needs to have its own static folder, which is ok for me but I also need to have a global static folder to serve resources that are common to all apps.
The problem is if I add to settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "staticfiles"),
]
I achieve the result of having a common global folder, but then the app-level static folders do not work anymore. Is there a way to keep both approaches? Thanks
I am as well very new to django, and dont't fully understand what I am saying, but it appears that I did the same thing and it somehow worked, STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
do you have this under INSTALLED_APPS = [] 'django.contrib.staticfiles' in settings.py, also are you using a database for your project

django static url with mezzanine and templates

I've been stuck for awhile and I can't seem to get it. Before you say python manage.py collectstatic yes, I've done that.
Here's the issue: I am using one of mezzanine's solid layout. It doesn't seem like the mezzanine's files merge with the solid template layout.
On my front page, the CSS works, but if i head over to /admin page, then the css loads like this admin/static/mezzanine/some_css.css. Anything but the root main page loads the css file. So I'm guessing it's that path, but I've double checked my root path, it seems correct?
settings.py
PROJECT_ROOT = BASE_DIR = os.path.dirname(PROJECT_APP_PATH)
# PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) also tried this
STATIC_URL ='static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, "solid/static"), ) also tried this
STATICFILES_DIRS = (
os.path.join(os.path.realpath(PROJECT_ROOT), "solid/static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
urls.py
urlpatterns +=[]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
simplfied file structure
app
---app
---solid
static
admin
css
mezzanine
templates
---static
admin
css
mezzanine
manage.py
You are missing an important thing named a slash in the begining. Put a slash in front.
STATIC_URL ='/static/'

Django - 404 static files not found

I know theres about a millon of these questions but non of them have helped me out.
I can't access my static files for deployment, I've slpit my settings file into base and production and put them in a folder in the settings.py orginal file.
I've done everything neccessary but it still doesn't seem to be working and I can't work it out for the life of me.
I've tried editing the path several time and no change.
Maybe I've missed something obvious and someone else can see it.
venv
--project
----app1
------static folder
----wsgi folder
------settings_old.py
------new settings folder
---------base.py
---------production.py
base.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = ['django.contrib.staticfiles',]
STATIC_URL = '/static/'
STATIC_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_r')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
I've tried a couple of different things like changing from BASE_DIR to a new PROD_DIR that directly goes to the static file in index.
PROD_ROOT = os.path.dirname(os.path.abspath('__file__' ))
PROD_ROOT= os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath('__file__' ))))
PROD_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
Nothing seems to be working.
UPDATE:
as suggested by other I've tried collectstatic, the folder env/project/static_r was found however, i get
'0 files where copied to static_r'.
furthermore, also as suggested I printed out base_dir and prod_dir and got the following
"prod is /user/venv/project" & "base is /user/venv/project/wsgi-folder"
p.s this is after I edited prod_dir to -
PROD_ROOT = os.path.dirname(os.path.abspath('__file__' ))
I am not really sure, but are you sure it is STATIC_DIRS and not STATICFILES_DIRS? '-'
Based on your settings, your static files should be located in /project/static/ instead of in the individual app.
You will also need to load the static files as stated in the documentation
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
What you can always do to test your paths is to print it.
so in your settings do
print BASE_DIR, STATIC_DIRS, STATIC_ROOT
and you'll see where your error is.
In my opinion it's the fact you're putting the static folder within the app, rather than within the project, because collectstatic will put all static files in one directory regardless

How can I get PyCharm to recognize the static files?

I have one Django project that looks like:
/.idea
/clients
/app
/static
coin.png
/templates
index.html
__init__.py
urls.py
/clients
settings.py
manage.py
In index.html I have (I can see the image on render):
{% load staticfiles %}
<img src="{% static 'coin.png' %}">
Relevant parts of settings.py:
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), '../static/'),
)
INSTALLED_APPS = (
'django.contrib.staticfiles',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.static',
)
In project structure I've added /clients since the root of the Django project is one level up from the repo root. However all my {% static %} uses in this project keep getting highlights as not existing even though Django can find them. Ideas on how to resolve this?
You need to set the following settings, to tell PyCharm where to find the project root:
Django settings to set (image)
You can also use {{STATIC_URL}}/path/to/files, which works different by pycharm
This worked wonders. It is a bit strange of pycharm though.
STATICFILES_DIRS = (
'static',
)
The above assumes that your static dir is called static right under your root. Now need to do os.path.join. I've tried and tested this.

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).

Categories

Resources