How can I use user uploaded files in my templates? (django)










3














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!










share|improve this question




























    3














    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!










    share|improve this question


























      3












      3








      3


      2





      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!










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 13 '12 at 13:35









      Shawn Chin

      58.4k14138164




      58.4k14138164










      asked Apr 13 '12 at 13:24









      Tony Kyriakidis

      80641935




      80641935






















          5 Answers
          5






          active

          oldest

          votes


















          3














          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.






          share|improve this answer




















          • thanks okm the url was my problem
            – Tony Kyriakidis
            Apr 16 '12 at 12:16


















          5














          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 ">





          share|improve this answer




















          • 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


















          1














          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',
          )





          share|improve this answer






















          • 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


















          1














          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 ">





          share|improve this answer






























            0














            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.






            share|improve this answer




















              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
              );



              );













              draft saved

              draft discarded


















              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









              3














              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.






              share|improve this answer




















              • thanks okm the url was my problem
                – Tony Kyriakidis
                Apr 16 '12 at 12:16















              3














              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.






              share|improve this answer




















              • thanks okm the url was my problem
                – Tony Kyriakidis
                Apr 16 '12 at 12:16













              3












              3








              3






              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.






              share|improve this answer












              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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
















              • 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













              5














              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 ">





              share|improve this answer




















              • 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















              5














              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 ">





              share|improve this answer




















              • 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













              5












              5








              5






              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 ">





              share|improve this answer












              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 ">






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 13 '12 at 14:01









              Chris Pratt

              152k20233298




              152k20233298











              • 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















              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











              1














              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',
              )





              share|improve this answer






















              • 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















              1














              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',
              )





              share|improve this answer






















              • 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













              1












              1








              1






              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',
              )





              share|improve this answer














              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',
              )






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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
















              • 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











              1














              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 ">





              share|improve this answer



























                1














                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 ">





                share|improve this answer

























                  1












                  1








                  1






                  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 ">





                  share|improve this answer














                  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 ">






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 '18 at 0:51









                  Unheilig

                  11.9k165383




                  11.9k165383










                  answered Nov 12 '18 at 0:34









                  Algebra

                  111




                  111





















                      0














                      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.






                      share|improve this answer

























                        0














                        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.






                        share|improve this answer























                          0












                          0








                          0






                          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.






                          share|improve this answer












                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 13 '12 at 13:30









                          cfedermann

                          2,7971423




                          2,7971423



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Syphilis

                              Darth Vader #20