Django image not showing up in template (not URLs issue) - django

I've added the neccessary elements to the URLs.py.
The image is NOT stored in the database, it's just a background image.
The source of the img folder is 'storiesapp/static/storiesapp/img'
Settings.py (i've been trying different things as shown)
STATIC_URL = '/storiesapp/static/storiesapp/'
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = [
"/storiesapp/static/storiesapp/img/",
"/static/storiesapp/img/",
"/storiesapp/img/",
"/img/",
]
STATICFILES_FINDERS =[
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# full path where all images are stored - os.path will create the directory regardless of the operating system
MEDIA_URL = '/media/'
#
.URLs.py
from django.conf import settings
from django.conf.urls.static import static
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
.
I'm trying all of the following
<link rel="apple-touch-icon" sizes="76x76" href="./assets/img/image.png">
<img src="static/storiesapp/img/image.png">
<img class='img-responsive' src="{{ MEDIA.URL }}{{ image.png }}" />

The answer is "<img src="{% static 'storiesapp/img/image.png' %}"/>".
It was the wrong syntax.
I was able to come to this solution because I realized that my CSS templates, which I can see are working, are also using the static folder so it couldn't be a problem with the way anything is set up.

Related

Django not rendering static image files localted in MEDIA directory

im relatively new to Django. I have an app where I specify MEDIA_URL and MEDIA_ROOT in settings.py file in project root. My pared down project structure looks like this:
/
../files
../files/media
../files/media/logo.png
../solid/settings.py
../solid/urls.py
/web/templates (contains all my HTML)
inside settings I have
MEDIA_ROOT = BASE_DIR / 'files'
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = BASE_DIR /'files'
in my template web/template/index.html I'm trying to reference it
<img src="{{% MEDIA_ROOT %}}/images/ourlogo.png" class="" alt="Logo" height="40">
In solid/urls.py I have:
urlpatterns = [
path('admin/', admin.site.urls),
path('',web_views.index,name="index"),
######################################################
path('login/',login_page,name="login"),
path('logout/',log_out,name="logot"),
path('register/',register,name="register"),
#######################################################
path('upload/',web_views.upload,name="upload"),
path('list/',web_views.list_uploads,name="list"),
path('details/<int:oid>', web_views.view_download,name='view_download'),
path('download/<str:filename>', web_views.download_file,name='download_file'),
#######################################################
path('paypal/', include("paypal.standard.ipn.urls")),
path('payment/',web_views.upgrade,name="payment"),
path('paypal-cancel/', PaypalCancelView.as_view(), name='paypal-cancel'),
path('paypal-return/', PaypalReturnView.as_view(), name='paypal-return'),
#######################################################
]+static('s/',document_root=settings.MEDIA_ROOT)
the image is not rendering.
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS =[
os.path.join(BASE_DIR,'project_name/static')
]
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
project_name/urls.py
add this two lines with urlspatterns
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
if want to get the image from database
<img src="{{ img.photo_2.url }}" alt="" class="img-fluid">
here "img" is the dictionary key name. photo_2 is the database field name. and to show the image you have put the .url after that.
static image
<link href="{% static 'img/favicon.png' %}" rel="icon">

Why can't I find my static folder in my project

I'm working on a Django project and I can't see my static folder, I'm also having a problem displaying images.
And when I inspect the image div in the src it's written unknown, Here's how I display the image from the admin
``<img class="rounded-circle account-profile" src="{{ user.Profile.profile_photo.url }}" alt="profile">``
User is the current user, the profile and then the name of the column.
My folder structure is route-folder( project-folder, media-folder and app-folder )
My static settings
```STATIC_URL = '/static/'
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = [
os.path.join(SITE_ROOT, "static/"),
]```
The Url:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I think you are trying to retrieve a media image not a static one. I recomend using Whitenoise package to serve static files.
Follow instruction in this tutorial: http://whitenoise.evans.io/en/stable/django.html
Check you have all this setup:
# Settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
In the templates, you should remember to add the {% load static %} to grant access to the actual files.

Static file collections - Logo navbar does not show up

I am trying to have an image (logo) on the navbar.
My project is called "mysite-project" (where manage-pyis), it contains the app "mysite".
In order to upload my static file I did the following:
1) mysite-project/mysite/settings.py
I added:
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/')
]
2) Created folders static and added my logo.png in:
mysite-project/static/mysite-project/logo.png
3) mysite-project/templates/base.html
{% load staticfiles %}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'home' %}">
<img src="{% static 'mysite/logo.png' %}" height=30 width=30 class="d-inline-block alighn-top" />
Code of Conduct
</a>
</nav>
4) In mysite-project/mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/', include('django.contrib.auth.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
HOWEVER the image does not show up. I think I have some issues in the settings.py for the folders but I cannot find where
The issue is in your STATICFILES_DIRS setting. If you join a path that has a leading slash then the result will "ignore" any preceding arguments and everything after will be relative to root
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/') # This will result in "/static/"
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static') # This will result in "<BASE_DIR>/static/"
]
STATIC_ROOT is the directory that you want to serve files from and the directory that collect_static will populate with all your static files, STATICFILES_DIRS is where Django will collect the files from. STATICFILES_DIRS shouldn't contain STATIC_ROOT. The usual layout for a project is something like this
myproject/ # The root of your repo
myproject/
myapp/
static/ # This is where you put app specific assets
...
static/ # This is where you put your generic static assets. Add this to STATICFILES_DIRS
...
static/ # This is STATIC_ROOT and where your files are served from after being collected
The default value for STATICFILES_FINDERS will look in STATICFILES_DIRS and every apps static directory. If you are using git, you should add the static folder at the root of your repo to .gitignore

Django Will Not Pull CSS Static File (404 Error) Even Though File Paths Look Correct

When I load my local site I cannot get the CSS files to load. I'm running Django 1.9 and python 3.4.2.
Here is my structure:
apps/
app1/
app2/
etc.
clients/
media/ #css, js, images, etc.
static/ #static files
templates/ #html templates
__init__.py
manage.py
settings.py
etc.
In my settings.py file I have:
STATIC_ROOT = os.path.join(BASE_DIR, 'clients', 'static')
STATIC_URL = 'clients/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'clients', 'media'),
]
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'clients', 'media')
And my html template that is calling the css files is as so:
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/primary_stylesheet.css' %}" />
I continue to get a 404 error that says it can't find the css file in:/clients/static/css/primary_stylesheet.css
In my settings.py file I have printed out my STATICFILES_DIRS and STATIC_ROOT and they both lead directly where they should. I've read through the documentation and tried multiple variations of DIRS and ROOT and don't understand why the css file is not pulling correctly - even "collectstatic" is collecting correctly.
I greatly appreciate any help and wisdom someone else has to give.
Thank you!
If you have DEBUG = True set then django won't actually pull your files from the /static/ folder - it finds and collects your staticfiles at runtime when you input the runserver command.
I think you'll find that if you use the default setting for STATICFILES_FINDERS your app will be able to serve your files:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
If you are running your server with python ./manage.py runserver, you need to set urls for both static and media files, check here: https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development
When I am starting a new project, I generally set my urls.py like this:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
url_patterns = [
url(r'^admin/', admin.site.urls),
# your url patterns go here
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This requires you to set STATIC_ROOT, STATIC_URL, MEDIA_ROOT and MEDIA_URL in your settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_files'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

How to manage media files on Windows?

I want to get media files for Django when developing application. But they just don't set up. This is my settings:
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR
STATICFILES_DIRS = (
STATIC_ROOT + '\\projectpackage\\static\\',
)
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + '\\projectpackage\\media\\'
Templatetag:
<img class="img-responsive" src="{{ project.image.url }}" alt="">
Urls:
urlpatterns = [
url(r'^profile/', include(profile.urls)),
url(r'', include(authentication.urls)),
url(r'^project/', include(project.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^', 'views.index', name='index'),
url(r'^markdown/', include('django_bootstrap_markdown.urls')),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
I can't see my mistake, because I've tried every other advices in other questions. And everything works bizzare. Static files are running good, media uploading to media/prj_img, but when I try to show image at template I've got such strange result:
<img class="img-responsive" src="/media/C%3A/Development/projectdirectory/projectpackage/media/prj_img/wallhaven-131_od9FWLX.jpg" alt="">
How could I fix this media error? This is strange, because everything looks right. Why there are full path in url?
Edit:
BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Also I've figured out that I've got wrong upload_to and changed it to prj_img. Now I've got following link:
<img class="img-responsive" src="/media/prj_img/wallhaven-24700.jpg" alt="">
But still it's not displating.
I have a lot of comments on your code.
STATIC_ROOT location is not appropriate. With your settings collectstatic command will put everything in your project's directory. Change it like so:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
But take care, that this folder is accessable (both permissions and django-settings) in production.
Use os.path.join to join folders:
STATICFILES_DIRS = (
os.path.join(STATIC_ROOT, 'projectpackage', 'static')
)
This url pattern must be the last one and contain $:
url(r'^$', views.index, name='index'), # do not pass strings
These two url patterns do the same (docs):
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
Moreover, you do not need them. Just ensure, that INSTALLED_APPS setting contains 'django.contrib.staticfiles'.