How can I use user uploaded files in my templates? (django)
So far I made it to the part where a user uploads an image but how can I use those images which are located in my media folder? Here is what I tried :
my view:
#I even tried to import MEDIA_ROOT to use it in my template...
#from settings import MEDIA_ROOT
def home(request):
latest_images = Image.objects.all().order_by('-pub_date')
return render_to_response('home.html',
#'latest_images':latest_images, 'media_root':MEDIA_ROOT,
'latest_images':latest_images,,
context_instance=RequestContext(request)
)
my model:
class Image(models.Model):
image = models.ImageField(upload_to='imgupload')
title = models.CharField(max_length=250)
owner = models.ForeignKey(User)
pub_date = models.DateTimeField(auto_now=True)
my template:
% for image in latest_images %
<img src=" MEDIA_ROOT image.image " width="100px" height="100px" />
% endfor %
and my settings.py MEDIA_ROOT and URL:
MEDIA_ROOT = '/home/tony/Documents/photocomp/photocomp/apps/uploading/media/'
MEDIA_URL = '/media/'
So again here is what I am trying to do: Use those images in my templates!
python django file-upload django-templates media-url
add a comment |
So far I made it to the part where a user uploads an image but how can I use those images which are located in my media folder? Here is what I tried :
my view:
#I even tried to import MEDIA_ROOT to use it in my template...
#from settings import MEDIA_ROOT
def home(request):
latest_images = Image.objects.all().order_by('-pub_date')
return render_to_response('home.html',
#'latest_images':latest_images, 'media_root':MEDIA_ROOT,
'latest_images':latest_images,,
context_instance=RequestContext(request)
)
my model:
class Image(models.Model):
image = models.ImageField(upload_to='imgupload')
title = models.CharField(max_length=250)
owner = models.ForeignKey(User)
pub_date = models.DateTimeField(auto_now=True)
my template:
% for image in latest_images %
<img src=" MEDIA_ROOT image.image " width="100px" height="100px" />
% endfor %
and my settings.py MEDIA_ROOT and URL:
MEDIA_ROOT = '/home/tony/Documents/photocomp/photocomp/apps/uploading/media/'
MEDIA_URL = '/media/'
So again here is what I am trying to do: Use those images in my templates!
python django file-upload django-templates media-url
add a comment |
So far I made it to the part where a user uploads an image but how can I use those images which are located in my media folder? Here is what I tried :
my view:
#I even tried to import MEDIA_ROOT to use it in my template...
#from settings import MEDIA_ROOT
def home(request):
latest_images = Image.objects.all().order_by('-pub_date')
return render_to_response('home.html',
#'latest_images':latest_images, 'media_root':MEDIA_ROOT,
'latest_images':latest_images,,
context_instance=RequestContext(request)
)
my model:
class Image(models.Model):
image = models.ImageField(upload_to='imgupload')
title = models.CharField(max_length=250)
owner = models.ForeignKey(User)
pub_date = models.DateTimeField(auto_now=True)
my template:
% for image in latest_images %
<img src=" MEDIA_ROOT image.image " width="100px" height="100px" />
% endfor %
and my settings.py MEDIA_ROOT and URL:
MEDIA_ROOT = '/home/tony/Documents/photocomp/photocomp/apps/uploading/media/'
MEDIA_URL = '/media/'
So again here is what I am trying to do: Use those images in my templates!
python django file-upload django-templates media-url
So far I made it to the part where a user uploads an image but how can I use those images which are located in my media folder? Here is what I tried :
my view:
#I even tried to import MEDIA_ROOT to use it in my template...
#from settings import MEDIA_ROOT
def home(request):
latest_images = Image.objects.all().order_by('-pub_date')
return render_to_response('home.html',
#'latest_images':latest_images, 'media_root':MEDIA_ROOT,
'latest_images':latest_images,,
context_instance=RequestContext(request)
)
my model:
class Image(models.Model):
image = models.ImageField(upload_to='imgupload')
title = models.CharField(max_length=250)
owner = models.ForeignKey(User)
pub_date = models.DateTimeField(auto_now=True)
my template:
% for image in latest_images %
<img src=" MEDIA_ROOT image.image " width="100px" height="100px" />
% endfor %
and my settings.py MEDIA_ROOT and URL:
MEDIA_ROOT = '/home/tony/Documents/photocomp/photocomp/apps/uploading/media/'
MEDIA_URL = '/media/'
So again here is what I am trying to do: Use those images in my templates!
python django file-upload django-templates media-url
python django file-upload django-templates media-url
edited Apr 13 '12 at 13:35
Shawn Chin
58.4k14138164
58.4k14138164
asked Apr 13 '12 at 13:24
Tony Kyriakidis
80641935
80641935
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
On development machine, you need to tell the devserver to serve uploaded files
# in urls.py
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
'document_root': settings.MEDIA_ROOT,
),
)
# in template, use sorl-thumbnail if your want to resize images #
% with image.image as img %
<img src=" img.url " width=" img.width " height=" img.height " />
% endwith %
# furthermore, the view code could be simplified as
from django.shortcuts import render
def home(request):
latest_images = Image.objects.order_by('-pub_date')
return render(request, 'home.html', 'latest_images':latest_images)
On production environment using normal filesystem storage, ensure the webserver has permission to write to MEDIA_ROOT. Also configure the webserver to read /media/filename
from correct path.
thanks okm the url was my problem
– Tony Kyriakidis
Apr 16 '12 at 12:16
add a comment |
If you use the url
attribute, MEDIA_URL
is automatically appended. Use name
if you want the path without MEDIA_URL
. So all you need to do is:
<img src=" some_object.image_field.url ">
Plus note that__unicode__
always return unicode version ofname
, use it when you want to usename
and type less in template.
– okm
Apr 13 '12 at 14:25
add a comment |
ImageField() has an url
attribute so try:
<img src=" MEDIA_URL image.image.url " />
You could also add a method to your model like this to skip the MEDIA_ROOT part:
from django.conf import settings
class Image(...):
...
def get_absolute_url(self):
return settings.MEDIA_URL+"%s" % self.image.url
Also I'd use upload_to like this for sanity:
models.ImageField(upload_to="appname/classname/fieldname")
Try these settings:
import socket
PROJECT_ROOT = path.dirname(path.abspath(__file__))
# Dynamic content is saved to here
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
if ".example.com" in socket.gethostname():
MEDIA_URL = 'http://www.example.com/media/'
else:
MEDIA_URL = '/media/'
# Static content is saved to here
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Using src=" MEDIA_ROOT ..." cannot work as this is a local file path which cannot be accessed from within the external client; hence, it is required to use MEDIA_URL instead.
– cfedermann
Apr 13 '12 at 13:34
I meant MEDIA_URL
– Hedde van der Heide
Apr 13 '12 at 13:38
I get an error : PROJECT_ROOT = path.dirname(path.abspath(file)) NameError: name 'path' is not defined
– Tony Kyriakidis
Apr 13 '12 at 15:12
You have to import it, from os import path
– Hedde van der Heide
Apr 14 '12 at 8:36
add a comment |
For Django version 2.2., try the following:
#You need to add the following into your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
Your URL mapping
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #add this
# And then use this in your html file to access the uploaded image
<img src=" object.image_field.url ">
add a comment |
You have to make the folder containing the uploaded images "web accessible", i.e. either use Django to serve static files from the folder or use a dedicated web server for that.
Inside your template code, you have to use proper MEDIA_URL
values.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10141668%2fhow-can-i-use-user-uploaded-files-in-my-templates-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
On development machine, you need to tell the devserver to serve uploaded files
# in urls.py
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
'document_root': settings.MEDIA_ROOT,
),
)
# in template, use sorl-thumbnail if your want to resize images #
% with image.image as img %
<img src=" img.url " width=" img.width " height=" img.height " />
% endwith %
# furthermore, the view code could be simplified as
from django.shortcuts import render
def home(request):
latest_images = Image.objects.order_by('-pub_date')
return render(request, 'home.html', 'latest_images':latest_images)
On production environment using normal filesystem storage, ensure the webserver has permission to write to MEDIA_ROOT. Also configure the webserver to read /media/filename
from correct path.
thanks okm the url was my problem
– Tony Kyriakidis
Apr 16 '12 at 12:16
add a comment |
On development machine, you need to tell the devserver to serve uploaded files
# in urls.py
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
'document_root': settings.MEDIA_ROOT,
),
)
# in template, use sorl-thumbnail if your want to resize images #
% with image.image as img %
<img src=" img.url " width=" img.width " height=" img.height " />
% endwith %
# furthermore, the view code could be simplified as
from django.shortcuts import render
def home(request):
latest_images = Image.objects.order_by('-pub_date')
return render(request, 'home.html', 'latest_images':latest_images)
On production environment using normal filesystem storage, ensure the webserver has permission to write to MEDIA_ROOT. Also configure the webserver to read /media/filename
from correct path.
thanks okm the url was my problem
– Tony Kyriakidis
Apr 16 '12 at 12:16
add a comment |
On development machine, you need to tell the devserver to serve uploaded files
# in urls.py
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
'document_root': settings.MEDIA_ROOT,
),
)
# in template, use sorl-thumbnail if your want to resize images #
% with image.image as img %
<img src=" img.url " width=" img.width " height=" img.height " />
% endwith %
# furthermore, the view code could be simplified as
from django.shortcuts import render
def home(request):
latest_images = Image.objects.order_by('-pub_date')
return render(request, 'home.html', 'latest_images':latest_images)
On production environment using normal filesystem storage, ensure the webserver has permission to write to MEDIA_ROOT. Also configure the webserver to read /media/filename
from correct path.
On development machine, you need to tell the devserver to serve uploaded files
# in urls.py
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
'document_root': settings.MEDIA_ROOT,
),
)
# in template, use sorl-thumbnail if your want to resize images #
% with image.image as img %
<img src=" img.url " width=" img.width " height=" img.height " />
% endwith %
# furthermore, the view code could be simplified as
from django.shortcuts import render
def home(request):
latest_images = Image.objects.order_by('-pub_date')
return render(request, 'home.html', 'latest_images':latest_images)
On production environment using normal filesystem storage, ensure the webserver has permission to write to MEDIA_ROOT. Also configure the webserver to read /media/filename
from correct path.
answered Apr 13 '12 at 14:06
okm
19.9k46479
19.9k46479
thanks okm the url was my problem
– Tony Kyriakidis
Apr 16 '12 at 12:16
add a comment |
thanks okm the url was my problem
– Tony Kyriakidis
Apr 16 '12 at 12:16
thanks okm the url was my problem
– Tony Kyriakidis
Apr 16 '12 at 12:16
thanks okm the url was my problem
– Tony Kyriakidis
Apr 16 '12 at 12:16
add a comment |
If you use the url
attribute, MEDIA_URL
is automatically appended. Use name
if you want the path without MEDIA_URL
. So all you need to do is:
<img src=" some_object.image_field.url ">
Plus note that__unicode__
always return unicode version ofname
, use it when you want to usename
and type less in template.
– okm
Apr 13 '12 at 14:25
add a comment |
If you use the url
attribute, MEDIA_URL
is automatically appended. Use name
if you want the path without MEDIA_URL
. So all you need to do is:
<img src=" some_object.image_field.url ">
Plus note that__unicode__
always return unicode version ofname
, use it when you want to usename
and type less in template.
– okm
Apr 13 '12 at 14:25
add a comment |
If you use the url
attribute, MEDIA_URL
is automatically appended. Use name
if you want the path without MEDIA_URL
. So all you need to do is:
<img src=" some_object.image_field.url ">
If you use the url
attribute, MEDIA_URL
is automatically appended. Use name
if you want the path without MEDIA_URL
. So all you need to do is:
<img src=" some_object.image_field.url ">
answered Apr 13 '12 at 14:01
Chris Pratt
152k20233298
152k20233298
Plus note that__unicode__
always return unicode version ofname
, use it when you want to usename
and type less in template.
– okm
Apr 13 '12 at 14:25
add a comment |
Plus note that__unicode__
always return unicode version ofname
, use it when you want to usename
and type less in template.
– okm
Apr 13 '12 at 14:25
Plus note that
__unicode__
always return unicode version of name
, use it when you want to use name
and type less in template.– okm
Apr 13 '12 at 14:25
Plus note that
__unicode__
always return unicode version of name
, use it when you want to use name
and type less in template.– okm
Apr 13 '12 at 14:25
add a comment |
ImageField() has an url
attribute so try:
<img src=" MEDIA_URL image.image.url " />
You could also add a method to your model like this to skip the MEDIA_ROOT part:
from django.conf import settings
class Image(...):
...
def get_absolute_url(self):
return settings.MEDIA_URL+"%s" % self.image.url
Also I'd use upload_to like this for sanity:
models.ImageField(upload_to="appname/classname/fieldname")
Try these settings:
import socket
PROJECT_ROOT = path.dirname(path.abspath(__file__))
# Dynamic content is saved to here
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
if ".example.com" in socket.gethostname():
MEDIA_URL = 'http://www.example.com/media/'
else:
MEDIA_URL = '/media/'
# Static content is saved to here
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Using src=" MEDIA_ROOT ..." cannot work as this is a local file path which cannot be accessed from within the external client; hence, it is required to use MEDIA_URL instead.
– cfedermann
Apr 13 '12 at 13:34
I meant MEDIA_URL
– Hedde van der Heide
Apr 13 '12 at 13:38
I get an error : PROJECT_ROOT = path.dirname(path.abspath(file)) NameError: name 'path' is not defined
– Tony Kyriakidis
Apr 13 '12 at 15:12
You have to import it, from os import path
– Hedde van der Heide
Apr 14 '12 at 8:36
add a comment |
ImageField() has an url
attribute so try:
<img src=" MEDIA_URL image.image.url " />
You could also add a method to your model like this to skip the MEDIA_ROOT part:
from django.conf import settings
class Image(...):
...
def get_absolute_url(self):
return settings.MEDIA_URL+"%s" % self.image.url
Also I'd use upload_to like this for sanity:
models.ImageField(upload_to="appname/classname/fieldname")
Try these settings:
import socket
PROJECT_ROOT = path.dirname(path.abspath(__file__))
# Dynamic content is saved to here
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
if ".example.com" in socket.gethostname():
MEDIA_URL = 'http://www.example.com/media/'
else:
MEDIA_URL = '/media/'
# Static content is saved to here
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Using src=" MEDIA_ROOT ..." cannot work as this is a local file path which cannot be accessed from within the external client; hence, it is required to use MEDIA_URL instead.
– cfedermann
Apr 13 '12 at 13:34
I meant MEDIA_URL
– Hedde van der Heide
Apr 13 '12 at 13:38
I get an error : PROJECT_ROOT = path.dirname(path.abspath(file)) NameError: name 'path' is not defined
– Tony Kyriakidis
Apr 13 '12 at 15:12
You have to import it, from os import path
– Hedde van der Heide
Apr 14 '12 at 8:36
add a comment |
ImageField() has an url
attribute so try:
<img src=" MEDIA_URL image.image.url " />
You could also add a method to your model like this to skip the MEDIA_ROOT part:
from django.conf import settings
class Image(...):
...
def get_absolute_url(self):
return settings.MEDIA_URL+"%s" % self.image.url
Also I'd use upload_to like this for sanity:
models.ImageField(upload_to="appname/classname/fieldname")
Try these settings:
import socket
PROJECT_ROOT = path.dirname(path.abspath(__file__))
# Dynamic content is saved to here
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
if ".example.com" in socket.gethostname():
MEDIA_URL = 'http://www.example.com/media/'
else:
MEDIA_URL = '/media/'
# Static content is saved to here
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
ImageField() has an url
attribute so try:
<img src=" MEDIA_URL image.image.url " />
You could also add a method to your model like this to skip the MEDIA_ROOT part:
from django.conf import settings
class Image(...):
...
def get_absolute_url(self):
return settings.MEDIA_URL+"%s" % self.image.url
Also I'd use upload_to like this for sanity:
models.ImageField(upload_to="appname/classname/fieldname")
Try these settings:
import socket
PROJECT_ROOT = path.dirname(path.abspath(__file__))
# Dynamic content is saved to here
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
if ".example.com" in socket.gethostname():
MEDIA_URL = 'http://www.example.com/media/'
else:
MEDIA_URL = '/media/'
# Static content is saved to here
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
edited Apr 13 '12 at 13:38
answered Apr 13 '12 at 13:32
Hedde van der Heide
14k104379
14k104379
Using src=" MEDIA_ROOT ..." cannot work as this is a local file path which cannot be accessed from within the external client; hence, it is required to use MEDIA_URL instead.
– cfedermann
Apr 13 '12 at 13:34
I meant MEDIA_URL
– Hedde van der Heide
Apr 13 '12 at 13:38
I get an error : PROJECT_ROOT = path.dirname(path.abspath(file)) NameError: name 'path' is not defined
– Tony Kyriakidis
Apr 13 '12 at 15:12
You have to import it, from os import path
– Hedde van der Heide
Apr 14 '12 at 8:36
add a comment |
Using src=" MEDIA_ROOT ..." cannot work as this is a local file path which cannot be accessed from within the external client; hence, it is required to use MEDIA_URL instead.
– cfedermann
Apr 13 '12 at 13:34
I meant MEDIA_URL
– Hedde van der Heide
Apr 13 '12 at 13:38
I get an error : PROJECT_ROOT = path.dirname(path.abspath(file)) NameError: name 'path' is not defined
– Tony Kyriakidis
Apr 13 '12 at 15:12
You have to import it, from os import path
– Hedde van der Heide
Apr 14 '12 at 8:36
Using src=" MEDIA_ROOT ..." cannot work as this is a local file path which cannot be accessed from within the external client; hence, it is required to use MEDIA_URL instead.
– cfedermann
Apr 13 '12 at 13:34
Using src=" MEDIA_ROOT ..." cannot work as this is a local file path which cannot be accessed from within the external client; hence, it is required to use MEDIA_URL instead.
– cfedermann
Apr 13 '12 at 13:34
I meant MEDIA_URL
– Hedde van der Heide
Apr 13 '12 at 13:38
I meant MEDIA_URL
– Hedde van der Heide
Apr 13 '12 at 13:38
I get an error : PROJECT_ROOT = path.dirname(path.abspath(file)) NameError: name 'path' is not defined
– Tony Kyriakidis
Apr 13 '12 at 15:12
I get an error : PROJECT_ROOT = path.dirname(path.abspath(file)) NameError: name 'path' is not defined
– Tony Kyriakidis
Apr 13 '12 at 15:12
You have to import it, from os import path
– Hedde van der Heide
Apr 14 '12 at 8:36
You have to import it, from os import path
– Hedde van der Heide
Apr 14 '12 at 8:36
add a comment |
For Django version 2.2., try the following:
#You need to add the following into your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
Your URL mapping
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #add this
# And then use this in your html file to access the uploaded image
<img src=" object.image_field.url ">
add a comment |
For Django version 2.2., try the following:
#You need to add the following into your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
Your URL mapping
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #add this
# And then use this in your html file to access the uploaded image
<img src=" object.image_field.url ">
add a comment |
For Django version 2.2., try the following:
#You need to add the following into your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
Your URL mapping
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #add this
# And then use this in your html file to access the uploaded image
<img src=" object.image_field.url ">
For Django version 2.2., try the following:
#You need to add the following into your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
Your URL mapping
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #add this
# And then use this in your html file to access the uploaded image
<img src=" object.image_field.url ">
edited Nov 12 '18 at 0:51
Unheilig
11.9k165383
11.9k165383
answered Nov 12 '18 at 0:34
Algebra
111
111
add a comment |
add a comment |
You have to make the folder containing the uploaded images "web accessible", i.e. either use Django to serve static files from the folder or use a dedicated web server for that.
Inside your template code, you have to use proper MEDIA_URL
values.
add a comment |
You have to make the folder containing the uploaded images "web accessible", i.e. either use Django to serve static files from the folder or use a dedicated web server for that.
Inside your template code, you have to use proper MEDIA_URL
values.
add a comment |
You have to make the folder containing the uploaded images "web accessible", i.e. either use Django to serve static files from the folder or use a dedicated web server for that.
Inside your template code, you have to use proper MEDIA_URL
values.
You have to make the folder containing the uploaded images "web accessible", i.e. either use Django to serve static files from the folder or use a dedicated web server for that.
Inside your template code, you have to use proper MEDIA_URL
values.
answered Apr 13 '12 at 13:30
cfedermann
2,7971423
2,7971423
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10141668%2fhow-can-i-use-user-uploaded-files-in-my-templates-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown