模型未显示:无法在站点中显示模型

2024-03-28 19:39:35 发布

您现在位置:Python中文网/ 问答频道 /正文

任何人都可以帮助我使用python django模型,以下是我的代码 models.py

class honeymoon(models.Model):

locationh = models.CharField(max_length=100)
imgh = models.ImageField(upload_to='locations')
detailh = models.TextField()

def __str__(self):
    return self.locationh

views.py

def top10_honeymoon(request):
context = {
    'posth': honeymoon.objects.all()
}
return render(request,'shop/honeymoon.html',context)

html

<div class="blog_list">
<h1 class="blog_heading"> Top 10 Destination For Honeymoon</h1><br><br>
<h2 class="blog_location">{{ posth.locationh }}</h2><br>
<img class="blog_img" src="{{ posth.imgh.url  }}"><br>
<p class="blog_details">{{ posth.detailh }}</p><br><br>
</div>

管理员

admin.site.register(honeymoon)

我试图建立一个模型,并试图从管理员博客中添加一些项目,但它没有在我的网站上显示任何内容,甚至没有显示错误。数据正在从管理面板上载,但未显示


Tags: py模型brselfreturnmodelsrequestdef
3条回答

试试这个:

<div class="blog_list">
<h1 class="blog_heading"> Top 10 Destination For Honeymoon</h1><br><br>
{% for post in posth.all %}
<h2 class="blog_location">{{ post.locationh }}</h2><br>
<img class="blog_img" src="{{ post.imgh.url  }}"><br>
<p class="blog_details">{{ post.detailh }}</p><br><br>
{% endfor %}
</div>

您必须在实例中循环才能看到它

{% for obj in posth %}
<h2 class="blog_location">{{ obj.locationh }}</h2><br>
<img class="blog_img" src="{{ obj.imgh.url  }}"><br>
<p class="blog_details">{{ obj.detailh }}</p><br><br>
{% endfor %}

Django无法迭代自身。这就是为什么您能够显示数据。 您必须使用for循环在网页中显示数据

<div class="blog_list">
    <h1 class="blog_heading"> Top 10 Destination For Honeymoon</h1><br><br>

    {% for item in posth %}     # add this
    <h2 class="blog_location">{{ item.locationh }}</h2><br>
    <img class="blog_img" src="{{ item.imgh.url  }}"><br>
    <p class="blog_details">{{ item.detailh }}</p><br><br>
   {% endfor %}     # add this

</div>

相关问题 更多 >