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" />
Related
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
I want to display user uploaded images in my template. I can't seem to find what I'm doing wrong.
Using Django 2.2 version. I tried what the documentation says (https://docs.djangoproject.com/en/2.2/howto/static-files/) but couldn't find any other solution after this one failed.
This is from settings.py file.
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
This is from my urls.py file in an app called 'signage'
EDIT 1: I changed my urlpatterns to this. Completely unrelated to solving the problem. I just thought it would look better.
urlpatterns = [
path('', views.screens_dashboard, name='screens_dashboard'),
path('new/', views.add_screen, name='add_screen'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is from my signage/models.py file
picture = models.ImageField(upload_to='screens/')
This is from my template
<img class="img-fluid" src="{{ screen.picture.url }}" alt="First slide">
EDIT 2: It shows this url when I view the source through the console 'http://127.0.0.1:8000/media/screens/screen1_51svMYv.png'. So it is supposed the way it is but doesn't show the image.
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/
I'm having trouble understanding and using Django's ImageField.
I have a model:
class BlogContent(models.Model):
title = models.CharField(max_length=300)
image = models.ImageField(upload_to='static/static_dirs/images/')
description = models.TextField()
My file system is currently:
src
|---main_project
|---app_that_contains_blog_content_model
|---static
|---static_dirs
|---images
When I run the server and go to the Admin page, I can add BlogContent objects. After choosing an image for the image field, the image has a temporary name. However, after I save this object I can't find the image in the folder specified by the upload_to path.
What is the correct way to do this?
Your image would be uploaded to the media folder, so it's better to change path in the model like images/, and they will be upload to media/images
In settings.py add this
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
In url.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [....
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And then, if you want to display all this image, use something like this
in view.py
BlogContent.objects.all()
And render it like this:
{% for img in your_object %}
<img src="{{ img.image.url }}" >
{% endfor %}
static in upload_to doesnot make sense, since user-uploaded images go into media/ folder.. you need these:
image = models.ImageField(upload_to='blog/%Y/%m/%d')
and all images land in:
media/blog/2016/01/02/img_name.jpg
you access it in template like this:
<img src="{{ blog.image.url }}">
in settings:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
You should use media path, instead static. See docs
Pls tell me. Why ../static/image.jpg show images, and ../media/image.jpg now show
Need write url? Need change Setting? i dont fiend answer in documentation.
pls help.
2 night search answer.
Need upload from admin-panel photo and show in templates.
<img src="{{ tovar.product_img.url }}">
To display image you need load the static files in your template before referencing them. This is how you should display a picture in your template:
{% load staticfiles %}
<img src="{% static 'image.jpg' %}">
This image needs to be stored in your static folder. ../YourApp/YourApp/static/image.jpg is where I keep my static folder. Obviously it would be better to structure it further with images folder inside the static folder etc..
In your settings file you need the following lines:
# Static asset configuration
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
This should do the trick for you.
In settings.py
MEDIA_ROOT = '/path/to/yourmediafolder/'
MEDIA_URL = '/media/' # whatever but it should same in `urls.py`
In urls.py
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
Then in template
<img src="{{ MEDIA_URL }}images/imagename.jpg"/>
Note: Here image should be as '/path/to/yourmediafolder/images/imagename.jpg'
Complete example:
I have an image test.jpg as '/home/me/test.jpg
MEDIA_ROOT = '/home/' # or /home/me/ but change url in image src
MEDIA_URL = '/media/'
#urls.py same as above
In template
<img src="{{ MEDIA_URL }}me/test.jpg"/> # or <img src="{{ MEDIA_URL }}test.jpg"/> as or condition above in MEDIA_ROOT.
Note that {{ MEDIA_URL }}me , no / between them because MEDIA_URL='/media/
You can test by:
http://domain.com/media/me/test.jpg # or http://domain.com/media/test.jpg as OR condition in MEDIA_ROOT
in local:
http://localhost:8000/media/me/test.jpg #in locally
This works (in the html img tag):
src="(left squiggly, percent) static poc.image.name (percent, right squiggly):
...and thanks to whoever pointed out that ImageField has a name in addition to a (useless) path and (useless) url e.g.:
>>> from nutr.models import *
>>> poc=POC.objects.get(pk=87)
>>> poc.name
'Edgar'
In Django==2.0.2 use {% get_media_prefix %}
From the docs