简体   繁体   中英

Does not display an image! Django

Does not display an image!

image is exists, dut django didn't display any images on page, how to fix this?

this is my models.py

class All_Images_Of_The_Series(models.Model): 
    to_series                               = models.ForeignKey(Series, on_delete=models.CASCADE, blank=True, default=None)
    image_of_all                            = models.ImageField(upload_to="previews/previews_for_series/", default=None,width_field="width_field", height_field="height_field")
    is_poster                               = models.BooleanField(default=False) 
    is_active                               = models.BooleanField(default=True)
    width_field                             = models.IntegerField(default=0)
    height_field                            = models.IntegerField(default=0)
    timestamp                               = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated                                 = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return "Active: |%s| and Poster: |%s| " % (self.is_active, self.is_poster)

    class Meta:
        ordering                            = ["-timestamp"]
        verbose_name                        = 'All_Images_of_the_Series'
        verbose_name_plural                 = 'All_Images_of_the_Series'

this is my views.py

def homeview(request, *args, **kwargs):
    full_path                       = All_Images_Of_The_Series.objects.all()
    context = {"full_path":full_path,}
    return render(request, 'home.html', context)

this is my template

<div class="col-sm-3 right-side">
                <div class="new-movies-block">
                    <a class="header" href="#/new/">
                        <div class="title">Новинки</div>
                    </a>
                    {% for one_to_one_path in full_path %}
                        <div class="movie-spacer" ></div>
                        <a class="new-movie" href="{{ one_to_one_path.to_series.get_absolute_url }}" title="{{ one_to_one_path.to_series.eng_name }}">
                            <div class="title-info">
                            {{ one_to_one_path.to_series.season_of_this_series.number_of_season }} сезон {{ one_to_one_path.to_series.number_of_series }} серия 
                            </div>
                            <div class="date">{{ one_to_one_path.to_series.timestamp_rus }}</div>
                            <img src="{{ one_to_one_path.image_of_all.url }}" class="img-responsive">
                        </a>
                    {% endfor %}
                </div>
            </div>

If you look at the element code, the images, then in the line "src" there is a path to the picture, and it's working! But does not display the picture itself!

<img src="/media/previews/previews_for_series/1.png" class="img-responsive">

this is error in console(f12)

GET http://127.0.0.1:8000/media/previews/previews_for_series/1.png 404 (Not Found)

just a quick thoughts, you don't show me the things you might need to work. For example if this if for development on the urls you need this https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development and on your settings you may need something like that

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

I hope it helps

Best

Perhaps you haven't added the media to your urls.py

From django.conf import settings

# your stuff

if settings.DEBUG:

urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT
    })
)

Do this !

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^series/', include("serials.urls", namespace='series')),
    url(r'^', include("serials.urls", namespace='homeview')),



]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM