【发布时间】:2021-05-25 16:08:14
【问题描述】:
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
models.py
class Product(models.Model):
name = models.CharField(max_length=250)
image = models.ImageField()
tables.py
import django_tables2 as tables
from .models import Product
class ProductTable(tables.Table):
class Meta:
model = Product
fields = ("image" ,"name")
views.py
在 views.py 中,我过滤模型对象并获取 QuerySet
import django_tables2 as tables
from .models import Product
from .tables import ProductTable
-----
table = ProductTable(new_objects) #new_objects is Product QuerySet
return render(request, 'home.html', {'table': table })
结果
我有一张桌子,但没有图片,只有链接。 如何显示图片?
【问题讨论】:
-
尝试在 urls.py 中添加:+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT ),看看这里:docs.djangoproject.com/en/3.2/howto/static-files
-
@HudsonBarroso 没有帮助。
标签: python python-3.x django django-tables2