Django won't display image from database in HTML - python

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/

Related

my Django website full style broken after move from localhost to shared hosting

My site can't locating where it's static and media file after upload on shared hosting. Here is my
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/project32/rootfolder/media/' #this is the path of my hosing media root file
STATIC_ROOT = '/home/project32/rootfolder/static/' #this is the path of my hosing static root file
root urls.py
urlpatterns = [
path('admin/', admin.site.urls),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
where I am doing mistake? why my website can't locating the static folder on my shared hosing ?
I also run python manage.py collectstatic
my problem solved after adding adding STATIC_URL path in my root urls.py
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
You should delete the 'STATIC_ROOT' line. You should use it this way.

uploded image not saved in media folder

settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '../media/images/'
urls.py
if settings.DEBUG:
# Use static() to add url mapping to serve static files during development (only)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
profile_image = models.ImageField(upload_to="../media/images")
file path:
-main_project
/app #django app
/media/images #media folder inside image folder
/main #django main app
/static/css #static folder for js,css,images,fonts
/templates #html templates
I have also tried other way but still, my image is not uploaded in media folder
Change MEDIA_URL and MEDIA_ROOT to:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
Once you define above you don't need to explicitly put media folder inside ImageField:
profile_image = models.ImageField(upload_to="images")
See the official docs about managing files
firstly change to this
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
and in you model
profile_image = models.ImageField(upload_to="images")
and i guess you are using form to upload the image so in that
<form method="POST" enctype="multipart/form-data">
use enctype and if still doesn't work for you try to update it from admin panel than tell me it update or not

Django. Media url not found

I have a django project based on django 1.11.
I uploaded media files and try to get them but instead I get nothong.
I read documentation and did exactly as it said.
Also I checked 'media' foleder permissions.
settings.py:
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('myapp.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Change your MEDIA_ROOT to MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') in settings.py and make sure your media file in project root directory.

How to load a static file in django2.0

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.

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