Images from ImageField in Django don't load in template

后端 未结 7 1389
温柔的废话
温柔的废话 2020-12-08 15:58

I\'m building a gallery using Django(1.5.1) on my local machine. In my Album model I have a ImageField. There is a view to show all images of an album. It works

相关标签:
7条回答
  • 2020-12-08 16:18

    Source: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user

     from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    you need to add to url pattern to serve uploaded files

    0 讨论(0)
  • 2020-12-08 16:24

    Within your details.html, change your

    img src="{{ image.image.url }}" height="420"
    

    To

    img src="your_app/media/{{ image.image.url }}" height="420"
    

    I hope this helps. If not I will be glad to provide more details.

    0 讨论(0)
  • 2020-12-08 16:31

    I took a little from each of the answers above. I was having the same problem as you. I was getting returns that were being directed from the current /blog/post/(media_url)/image.jpg

    In my admin portal I could view it easily and edit it. But on my post.html I was having problems until I added the {{ MEDIA_URL }} --

    That is all that I was missing.

    I posted my entire section below so that other people could read it and see what they are missing.

    post.html:
    
        <label for="id_image">  <img src="{{ MEDIA_URL }}{{ p.image.url }}" 
        title="{{ p.title }}"> </label>
    
    models.py:
    
        from django.core.files.storage import FileSystemStorage
    
        upload_location =
        FileSystemStorage(location='/home/pi/djcode/xpcpro/xpcpro/images')
    
        class Blog(models.Model):
            title = models.CharField(max_length=255)
            author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
            date = models.DateTimeField(auto_now=False, auto_now_add=True)
            body = models.TextField()
            image = models.ImageField(
                storage=upload_location, null=True,
                blank=True, width_field="width_field",
                height_field="height_field",)
            height_field = models.IntegerField(default=0)
            width_field = models.IntegerField(default=0)
    
    urls.py:
    
        from django.conf.urls.static import static
        from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    
        urlpatterns += staticfiles_urlpatterns()
        urlpatterns += static(settings.MEDIA_URL,
        document_root=settings.MEDIA_ROOT)
    
    settings.py:
    
        MEDIA_ROOT = "/home/pi/djcode/xpcpro/xpcpro/images/"
        MEDIA_URL = "/images/"
    
    0 讨论(0)
  • 2020-12-08 16:32

    I have a clue on what's the problem. MEDIA_URL should be like this:

    MEDIA_ROOT='<the full path to your media folder>' (i.e: '/home/ike/project/media/')
    MEDIA_URL='/media/'
    

    Note the slash character at the beginning. That is because media is a folder in your root server folder and not relative to whatever other url you call it.

    And add these lines to the end of your urls.py file:

    # You might need to import static function like this:
    #from django.contrib.staticfiles.urls import static
    
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    You can check the following documentation: https://docs.djangoproject.com/en/dev/howto/static-files

    Hope this helps

    0 讨论(0)
  • 2020-12-08 16:35

    If you're using the dev server then you need to add something to your urls.py to make django serve the media files, cf:

    1.4.x : https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-other-directories 1.5.x: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user

    0 讨论(0)
  • 2020-12-08 16:36

    Check in your settings.py you have define MEDIA_ROOT and 'MEDIA_URL' (and they are correct). The MEDIA_ROOT specifies an absolute folder on your machine where media will be stored.

    So for an example:

    MEDIA_ROOT = '/myfolder/'
    

    This would mean it would look for image at:

    /myfolder/images/albums/
    

    Next in your settings.py check your MEDIA_ROOT location: i.e.

    MEDIA_URL = 'http://localhost/myfolder/'
    

    So your images:

    <img src="{{ MEDIA_URL }}{{ image.image.url }}" height="420"></a>
    

    This would relate to:

    http://localhost/myfolder/images/albums/
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题