我想将数据从“关于”模型传递到html模板,但是我不知道出了什么问题。我花了很多时间..但它仍然保持不变:
from django.db import models
class About(models.Model):
image = models.ImageField(upload_to = 'pics')
desc = models.TextField()
views.py
from django.shortcuts import render
from .models import About
def index(request):
abt = About.objects.all()
return render(request,'home/index.html',{abt:'abt'})
html
<img src="{{abt.image}" alt="profile photo">
<p>{{abt.desc}}</p>
{abt:'abt'}
不,这是另一回事。
{'abt':abt}
标签在左侧,数据在右侧。
如果要获得一个实例,可以使用first
method
<p>{{abt.first.desc}}</p>
或以类似方式,仅提供第一个对象
return render(request,'home/index.html',{'abt': abt.first()})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句