我在Django项目上工作,我想显示一些图像到模板,但是尽管可以上传它们,但无法显示它们。
class Sindiasmoi(models.Model):
sindid = models.AutoField(db_column='sindID', primary_key=True, db_index=True) # Field name made lowercase.
descr = models.CharField(max_length=100)
shortdescr = models.CharField(db_column='shortDescr', max_length=50) # Field name made lowercase.
photo = models.ImageField(db_column='photo',upload_to='sindiasmoi',default='elections.jpg',null=True, blank=True)
eidos = models.IntegerField(default=1)
def __str__(self):
return self.descr
class Meta:
managed = True
db_table = 'SINDIASMOI'
我的看法是:
def pososta_telika(request, eklid):
# φιλτράρισμα επιλεγμένης εκλ. αναμέτρησης
selected_ekloges = Eklogestbl.objects.get(eklid=eklid)
# επιλογή όλων των εκλ. αναμετρήσεων με visible=1 και κάνω φθίνουσα ταξινόμηση αν δεν δοθεί παράμετρος
all_ekloges = Eklogestbl.objects.filter(visible=1).order_by('-eklid')
#ανάκτηση εγγραφών επιλεγμένης εκλ. αναμέτρησης από το σχετικό database view
all_pososta = EklSumpsifodeltiasindVw.objects.filter(eklid=eklid).order_by('-posostosindiasmou')
context = {'all_pososta':all_pososta, 'all_ekloges':all_ekloges, 'selected_ekloges':selected_ekloges.eklid}
return render(request, 'Elections/pososta_telika.html',context)
我的模板代码具有以下代码:
{% for p in all_pososta %}
<img src="{{ p.sindid.photo}}" alt="image"/>
{% endfor %}
我也在我的settings.py中添加了以下行:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
最后,我的urls.py的代码如下:
urlpatterns = [
path('admin/', admin.site.urls),
path(r'', include('Elections.urls')),
path('admin_tools/', include('admin_tools.urls')),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
# For django versions before 2.0:
# url(r'^__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
我尝试将{{p.sindid.photo}}替换为{{p.sindid.photo.url}},但这会引发错误,例如“'bytes'对象没有属性'url'” >
那我在做什么错了?
答案 0 :(得分:0)
此外,下面是我的模型EklSumpsifodeltiasindVw,如果有帮助的人:
class EklSumpsifodeltiasindVw(models.Model):
id = models.IntegerField(primary_key=True)
eklid = models.ForeignKey(Eklogestbl, models.DO_NOTHING, db_column='eklID', db_index=True) # Field name made lowercase.
sindid = models.ForeignKey(Sindiasmoi, models.DO_NOTHING, db_column='sindID', db_index=True) # Field name made lowercase.
sindiasmos = models.CharField(max_length=100)
shortdescr = models.CharField(db_column='shortDescr', max_length=50) # Field name made lowercase.
photo=models.ImageField(db_column='photo',null=True)
sumvotes = models.DecimalField(db_column='sumVotes', max_digits=32, decimal_places=0, blank=True, null=True) # Field name made lowercase.
sinola = models.DecimalField(max_digits=32, decimal_places=0, blank=True, null=True)
katametrimena = models.BigIntegerField(blank=True, null=True)
plithoskentrwn = models.BigIntegerField(db_column='plithosKentrwn') # Field name made lowercase.
posostosindiasmou = models.DecimalField(db_column='posostoSindiasmou', max_digits=38, decimal_places=2, blank=True, null=True) # Field name made lowercase.
posostokatametrimenwnkentrwn = models.DecimalField(db_column='posostoKatametrimenwnKentrwn', max_digits=26, decimal_places=2, blank=True, null=True) # Field name made lowercase.
sumvotesb = models.DecimalField(db_column='sumVotesB', max_digits=32, decimal_places=0, blank=True, null=True) # Field name made lowercase.
sinolab = models.DecimalField(db_column='sinolaB', max_digits=32, decimal_places=0, blank=True, null=True) # Field name made lowercase.
posostosindiasmoub = models.DecimalField(db_column='posostoSindiasmouB', max_digits=38, decimal_places=2, blank=True, null=True) # Field name made lowercase.
katametrimenab = models.BigIntegerField(db_column='katametrimenaB', blank=True, null=True) # Field name made lowercase.
posostokatametrimenwnkentrwnb = models.DecimalField(db_column='posostoKatametrimenwnKentrwnB', max_digits=26, decimal_places=2, blank=True, null=True) # Field name made lowercase.
sumvotesk = models.DecimalField(db_column='sumVotesK', max_digits=32, decimal_places=0, blank=True,
null=True) # Field name made lowercase.
sinolak = models.DecimalField(db_column='sinolaK', max_digits=32, decimal_places=0, blank=True,
null=True) # Field name made lowercase.
posostosindiasmouk = models.DecimalField(db_column='posostoSindiasmouK', max_digits=38, decimal_places=2,
blank=True, null=True) # Field name made lowercase.
katametrimenak = models.BigIntegerField(db_column='katametrimenaK', blank=True,
null=True) # Field name made lowercase.
posostokatametrimenwnkentrwnk = models.DecimalField(db_column='posostoKatametrimenwnKentrwnK', max_digits=26,
decimal_places=2, blank=True,
null=True) # Field name made lowercase.
这是一个mysql数据库视图。
此外,我在浏览器中显示的有关图像的HTML代码是:
<img src="b'DSC00176.JPG'" alt="image"/>
<img src="b'sindiasmoi/DSC00239_7GGlF7a.JPG'" alt="image"/>
<img src="b'DSC00165.JPG'" alt="image"/>
<img src="b'wwf_earth_hour_2018.jpg'" alt="image"/>
<img src="b'sindiasmoi/DSC00240.JPG'" alt="image"/>
<img src="b'sindiasmoi/DSLSPEED07102017_cfQWRL6.PNG'" alt="image"/>
我认为路径有些问题。
答案 1 :(得分:0)
好,问题解决了!
问题是这一行: photo = models.ImageField(db_column ='photo',null = True)
我不应该包含“ db_column ='photo'”。当我删除此参数时,一切正常。