Django can't display images saved in media folder - python

I save uploaded files in a media root called /img/:
MEDIA_ROOT = os.path.join(BASE_DIR, 'img')
MEDIA_URL = '/img/'
And use this template to display every image in that folder:
{% load staticfiles %}
<ul>
{% for post in latest_post %}
<li>{{ post.id }} : {{ post.post_body }} : <img src="{% static "/img/" %}{{ post.post_image }}" alt="{{ post.post_image }}" /> </li>
{% endfor %}
</ul>
And I get the right url:
http://127.0.0.1:8000/img/birthday.jpg
But I get "page not found" error when I open the image. Why is that?
Edit: I just ran manage.py collectstatic but it didn't fix the issue. I still get 404 error.

Create a folder in your base directory by the name static and store all your css, img, js etc files in it in their respective folders, like this:
.
├── css
├── fonts
├── icons
├── img
└── js
Run python manage.py collectstatic, this collects all your static files and saves them in a staticroot folder.
After that change your settings.py to the following:
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static')),
STATIC_ROOT = 'staticroot/static'

Related

Django static files not found Value error

I'm working on a project using cookiecutter django template: https://github.com/pydanny/cookiecutter-django
The project is run in docker containers that come with the cookiecutter-django template on ubuntu 16.04LTS.
When trying to get the site to production, it returns the following error on some pages:
the file 'events\css\themes\fontawesome-stars.css' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f830be38ac8>.
folder structure is:
./project/events/static/
└── events
├── css
   ├── details.css
   ├── list.css
   └── themes
   ├── fontawesome-stars.css
  └── fontawesome-stars-o.css
No errors are reported during docker build process and after that running collectstatic.
Permissions for the files on the server are set to 775.
static config in base.py config:
# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR('staticfiles'))
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'
# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
str(APPS_DIR.path('static')),
]
# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
In template I'm including the file like this.:
{% load static %}
{% load crispy_forms_tags %}
{% block title %}
{% endblock%}
{% block css %}
{{block.super}}
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'events\css\themes\fontawesome-stars.css' %}">
{% endblock %}
How are you including the static files on your templates? It looks you are specifying the path directly. Instead you should use:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'events/css/themes/fontawesome-stars.css' %}">
Because in production whitenoise and collectstatic command will add extra content to the file name for versioning, caching and other purposes.

Media Url not resolving properly

I have the media url and media root as follows.
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_URL)
my urls.py is
if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am accessing it in the template as follows:
<img class="hl" src="{{ MEDIA_URL }}prop/image0.png" /></a>
The url replaced when rendered is correct, which is /media/prop/image0.png.
But it says the media location is not found.
I suggest you use staticfile for this purpose, because you are using a specific image and not something that will be uploaded in the future.
If you decide to use static files, put the image in your static folder and then use this:
<img src="{% static 'image0.png' %}" style="">
Don't forget to load your staticfiles:
{% load staticfiles %}
By the way, for future, if you wanted to use media files in your templates you have to do it like this:
{% for image in images %}
<img src="{{ image.url }}" class="h1">
{% endfor %}

Serving static files in django 1.8

My 404.html page does not reference my finale.css file.
My directory structure
musicworld/
musicworld/
__pycache__
__int__.py
settings.py
urls.py
wsgi.py
feature/
__pycache__
migrations
static/
feature/
finale.css
templates/
feature/
about.html
detail.html
404.html
500.html
index.html
template.html
__init__.py
admin.py
models.py
tests
This is where in index.html I'm referencing the css
<link rel="stylesheet" type="text/css" href="{% static 'feature/finale.css' %}" />
But 404.html that extends index.htmlis not referencing the css
{% extends "index.html" %}
{% load staticfiles %}
{% block 404page %}
<div class="box">
<p class="box-message" data-dead-message="AAAAAHHHHHH!">Oh No! Something went wrong.Punish the developer by clicking here.</p>
</div>
<div class="robot">
<img src="" alt="Robot" />
</div>
{% endblock %}
static reference in settings.py
STATIC_URL = '/static/'
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
Both the index.htmland template.html are placed in the same folder and are properly referencing the css.Plus all the html pages in feature that are extending index.html are also referencing the css.But both 404.html and 500.htmlare not.

how to display images?

please help bring images into the template
in django1.6 I uploaded via the form in the database image. screenshot. ( checked that the images loaded in a specific directory ) . then imported into the template file settings.py variable BASE_DIR and to all records of a table . then tried in the template to display the image as follows:
{% for entrie in all_entries_carousel %}
<a href="{{ entrie.link }}" title="{{ entrie.title }}" target="_blank">
<img src="{{ BASE_DIR }}/{{ entrie.image }}" width="300" height="200" alt="{{ entrie.title }}" />
</a>
{% endfor %}
results in images that I have not loaded and displayed.
in the source path
c:\Python33\django_projects\proj1/carousel/media/images/img1.png
please tell me how can I still display the image. Sure , there is a path without importing BASE_DIR
ps this way does not work proj1/carousel/media/images/img1.png
You need to configure your static files.
From Django docs:
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL = '/static/'
In your templates, either hardcode the url like
/static/my_app/myexample.jpg or, preferably, use the static template
tag to build the URL for the given relative path by using the
configured STATICFILES_STORAGE storage (this makes it much easier
when you want to switch to a content delivery network (CDN) for
serving static files).
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
Store your static files in a folder called static in your app. For
example my_app/static/my_app/myimage.jpg.

Django template image won't load (repost) [duplicate]

This question already has answers here:
Django template image won't load
(4 answers)
Closed 9 years ago.
Image wan't load.I see o broken image icon.I use the static tag. I also tried giving the absolute path and never worked.I used the alt tag but still i can only see the broken image icon. Is the tag correct what else should i try ??? The images is in static\images\logo.png
base.html
{% load static %} {# loads static tag #}
<div id="header">
{% block header %}
<img src="{% static 'images/logo.png' %}" alt="alternative text" />
{% endblock %}
</div>
setting.py:
MEDIA_ROOT = ''
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('assets','C:\Users\#########\workspace\project\static'),
Try either
<img src="{{ STATIC_URL }}images/logo.png" alt="alternative text" />
or fixing your quotes
<img src="{% static 'images/logo.png' %}" alt="alternative text" />
and fixing your settings
STATICFILES_DIRS = ('assets', r'C:\Users\#########\workspace\project\static'),
or
STATICFILES_DIRS = ('assets','C:\\Users\\#########\\workspace\\project\\static'),
It's worth asking whether or not you've used Django's collectstatic method after including the image in your assets folder.

Categories