How to load a static file in django2.0 - python

I am having difficulty loading static files in django2.0 (coming from django1.4). Here is what I have so far:
# urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# settings.py
TEMPLATES = [
...
'builtins': [
'django.contrib.staticfiles.templatetags.staticfiles',
],
]
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = ''
STATICFILES_DIRS = [
os.path.join(SITE_ROOT, "static"),
]
And I have one image located at:
[my_project_root]/static/img/image.png
Now if I go directly to the url, I get a 404:
http://localhost:8000/static/img/image.png
Additionally, if I do it "through the template", I also get a 404:
{% load static %}
<img src="{% static 'img/image.png' %}" alt="My image">
What do I need to add here to serve the static files?

This was a tricky setting. I needed to change the STATIC_ROOT to:
STATIC_ROOT = 'static/'
Otherwise, with STATIC_ROOT = '', it would look for the img file in [project_root]/img/image.png.

Related

404 error when getting file in static, django

I tried several attempts through googling, but it didn't work. I don't think it was like this when I was working on another Jango project, but I don't know why.
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.HomeView.as_view(), name = 'home'), #to home.html
path("blog/", include('blog.urls')),
]
# urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "static/"
STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),)
# STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static")
home.html
<img src="{% static 'assets/img/navbar-logo.svg' %}" alt="..." />
directory
Project-folder
|
|__config
| |__settings.py
| |__urls.py
|
|__app
|
|__template
| |__app
| | |__bla.html
| |__home.html
|
| #bootstrap
|__static
|__assets
|__img
...
An Excerpt from the docs.
Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.
So, use list of dictionaries and also it is STATICFILES_DIRS not STATICFILES_DIR, you missed S.
Try this:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static"
]
Or this:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Make sure that you have loaded the static tag using % load static %.
If you've didn't loaded the static try this in your base.html or the home.html
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">

Templates do not show image. Django

Here is what i tried to do:
in my settings.py:
TEMPLATES = [
...
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.media',
],
},
},
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'img')
MEDIA_URL = '/img/'
i have also added this in my main urls.py:
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here's the code in my template:
<img
src="{{ product.image.url }}"
alt="Not available"
height="188px"
style="margin: 10px 0"
width="188px"
/>
Edit ...
I solved my problem :)
This is what is did
src="{{ MEDIA_URL }}{{ product.image.url }}"
i added {{ MEDIA_URL }} in src of my <img> tag.
In my project, I have added main urlpatterns as :
urlpatterns = urlpatterns + \
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And it working fine.
Try using this for your static file resource
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
Move your images to a folder name images inside your static folder
Then call your image in this way in your template file
<img src="{% static 'images/your_image.png' %}">

Django not able to load a simple static page with static files

It is a simple static html file with only one image as a static file in the html. I have followed all the steps provided in the Django documentation, and the collectstatic command runs without any error.
The project folder structure is as follows;
settings.py file;
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
DEBUG = True
home.html - File where the image is not loading;
{%load static%}
<!DOCTYPE html>
<html >
<head>
<title>home.html</title>
<h1>This is a test file</h1>
</head>
<body>
<img src="{% static 'images/test.png' %}">
</body>
</html>
Output:
I have tried the following;
Restart server,
rerun the collectstatic command
Nothing worked out, What could be the issue? Please help me to resolve this issue.
replace STATICFILES_DIR with STATICFILES_DIRS in settings.py
You have STATIC_ROOT = os.path.join(BASE_DIR, 'assets') whereas it looks like you've already collected your files to os.path.join(BASE_DIR, 'static'), which is a more typical location. You should change STATIC_ROOT to point back to the static folder so that Django knows where to serve those files from:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
You can see this in the static files deployment guide
try this in your settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
def get_success_url(self, request, user):
return (get_success_url)
and add this from your urls.py
urlpatterns = [
......
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns +=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
STATICFILES_DIR is the main error here.
Replace it with STATICFILES_DIRS
This should solve it.

Django won't display image from database in HTML

Hi I am trying to get user profile images to display from a Django MySQL database corresponding to the user that is logged in..
Here is my settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
# Media files
MEDIA_ROOT = MEDIA_DIR
#MEDIA_DIRS = [MEDIA_DIR, ]
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/admin/media/'
Here is my urls.py:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^smartcity/',include('smartcity.urls')),
#url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
#url(r'^accounts/register/$', views.register, name='registration'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', admin.site.urls),
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]
And this is what I have in the html file:
src="{% static 'user.userprofile.picture.url' %}"
This is my table i am trying to retrieve the picture from:
Database screenshot
I'm not sure how to get it to display, I thought my URL mapping was correct as i can view the image if i go to http://127.0.0.1:8000/media/profile_images/trump.jpg
Any ideas? Sorry I am a bit of noobie.
FileField uploads to the media side of things, not static, so you just have to do;
<img src="{{ user.userprofile.picture.url }}" alt="" />
Here is an example of this scenario; https://www.simplifiedpython.net/django-file-upload-tutorial/

Cant see image from project directory

I tried to handle this but I gave up. I have folder with images and I want to display my some image in html view but It wont work.
I followed this tutorial enter link description here
this my project tree:
As you can see I tried to create plenty of directories to make it work.
This is my settins:
STATIC_URL = '/static/'
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "media"),
# '/webstore/',
# ]
MEDIA_ROOT = '/webstore/media/'
MEDIA_URL = '/media/'
this is my html view where I try to display my image
<img src="/media/example.jpg" />
this is my urls.py file
from django.conf.urls import url, include
from django.contrib import admin
from djangoproject import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^', include('webstore.urls')),
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
MEDIA_URL is the base URL to serve the media files uploaded by users, and MEDIA_ROOT is the local path where they reside.
so try to use it on your setting.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
and on your main urls.py
urlpatterns = [
....
]
if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and should the entire image will be saved in your djangoproject/media/
one more thing don't forget to add {% load staticfiles %} on top your html file. {% load staticfiles %} tells Django to load the staticfiles template tags that are provided by the django.contrib.staticfiles application.
MEDIA_ROOT is the absolute filesystem path to the directory that will hold user-uploaded files.
STATIC_ROOT is the absolute filesystem path to the directory from which you’d like to serve these files.
Since, you would like to serve images, give the absolute path of your static directory to STATIC_ROOT.
Give relative path with respect to your STATIC_ROOT in STATIC_URL.
Also, change your urls.py with the static_url and static_root.
Another Suggestion: Easier way to display an image is to upload it to image servers like imgur and give the url of the image in the html.
For e.g. :
<img src="http://i.stack.imgur.com/nbegK.png" />
The only thing you have to do is
MEDIA_ROOT = '/absolutepath/to/djangoproject/webstore/media/'
Then you have already
MEDIA_URL = '/media/'
and try
<img src="/media/example.jpg" />

Categories

Resources