首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >{{ obj.photos.url }}未显示图片

{{ obj.photos.url }}未显示图片
EN

Stack Overflow用户
提问于 2020-05-20 21:34:05
回答 2查看 75关注 0票数 0

-根urls.py

代码语言:javascript
复制
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('estate.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

- app urls.py

代码语言:javascript
复制
from django.urls import path
from . import views
from .views import index, about, category, PostListView, PostDetailView


urlpatterns = [
    path('', views.index, name='index'),
    path('about/', views.about, name='about'),
    path('category/', views.category, name='category'),
    path('liste/', PostListView.as_view(), name='liste'),
    path('detail/<int:id>/', PostDetailView.as_view(), name='detail-list'),

代码语言:javascript
复制
class lists(models.Model):
    ...
    photos = models.ImageField(upload_to='photos/')
    ...
    def __str__ (self):
    return self.title

代码语言:javascript
复制
    from django.shortcuts import render, get_object_or_404
    from django.views.generic import ListView, DetailView
    from .models import *

    def index(request):
        listings = lists.objects.filter(is_active=True)
        hit10 = lists.filter().order_by('-hit_counter')[:10]


        context = {
            'listings':listings, 
            'hit10':hit10,
            }

        return render(request, 'index.html', context)


    class PostListView(ListView):
        queryset = lists.objects.filter(is_active=True)
        paginate_by = 5


    class PostDetailView(DetailView):

        def get_object(self):
            id_=self.kwargs.get("id")
            return get_object_or_404(listings, id=id_)

代码语言:javascript
复制
{% extends 'base.html'%}
{% load static %}

{% block content %}


<table class="table table-striped">
    <thead>
        <tr>
        <th scope="col">ID</th>
        <th scope="col">Photo</th>
        <th scope="col">Title</th>
        <th scope="col">Date</th>
        </tr>
    </thead>
    {% for obj in object_list %}
        {% if obj.is_active %}
        <tbody>

            <tr>
            <th scope="row">{{ obj.id }}</th>
            <td>{{ obj.photo.url }}</td>
            <td><a href="{% url 'detail-list' obj.id %} ">{{ obj.title }}</a></td>
            <td>{{ obj.date }}</td>

            </tr>
        {% endif %}
    {% endfor %}

    </tbody>
</table>


{% endblock content %}

没有THUMBNAIL显示图像的结果是这样的: /media/photo/01.jpg

其他的都很好。谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-05-21 16:00:49

图像不会显示,因为您没有将url放在图像html标记中。已修改template.html文件:

代码语言:javascript
复制
...
<td> <img src=/static/{{ obj.photo.url }}></td>
...

注意:‘static’应该是你在settings.py中设置的STATIC_URL。

票数 0
EN

Stack Overflow用户

发布于 2020-05-22 22:52:22

我找到了原因;

在template.py中

代码语言:javascript
复制
    <tr>
                                 <th scope="row">{{ obj.id }}</th>
 THIS CODE SHOULD BE --->        <td>{{ obj.photo.url }}</td>
                                 <td><a href="{% url 'detail-list' obj.id %} ">{{ obj.title }}</a></td>
                                 <td>{{ obj.date }}</td>
                                 </tr>





     <tr>
                       <th scope="row">{{ obj.id }}</th>
 LIKE THIS ONE --->    <td><img  style="width:50%;" class="img-thumbnail" src="{{obj.photo.url}}" alt="IMG"></td>
                       <td><a href="{% url 'detail-list' obj.id %} ">{{ obj.title }}</a></td>
                       <td>{{ obj.date }}</td>
                                 </tr>

感谢所有提供帮助的人,特别是@Adil Shirinov。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61914460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档