相关文章推荐
星星上的墨镜  ·  触发器精讲·  2 月前    · 
火爆的春卷  ·  ElFinder与CodeIgniter ...·  1 年前    · 
玩篮球的柠檬  ·  yarn.lock、package-lock ...·  1 年前    · 
千杯不醉的电脑桌  ·  Logitech LUA脚本: ...·  1 年前    · 

如何在Django模板中显示外键数据

0 人关注

我有 Project Tags 之间的关系,一个项目可以有多个标签,但我无法在我的模板中显示标签数据,我试图根据标签的名称来显示数据。但我得到了错误,请让我知道如何在我的模板中显示标签数据。

Here is my urls.py file...

    path('tag/<tag_slug>', views.projecttag, name='projecttag'),

here is my `models.py file...

class Tags(models.Model):
    project = models.ForeignKey(Project, null=True, blank=True, related_name='ProjectTags', on_delete=models.CASCADE)
    tag_name = models.CharField(max_length=64, null=True, blank=True)
    tag_slug = models.SlugField(max_length=64, null=True, blank=True)

here is my views.py file...

def projecttag(request, tag_slug):
    tag = get_object_or_404(Tags, tag_slug=tag_slug)
    project = Project.objects.filter(ProjectTags=tag)
    context = {'tag':tag, 'project':project}
    template_name = 'pages/tags.html'
    return render(request, template_name, context)

here is my tags.html file...

{% for property in project %}
    <div class="single-property-box">
        {{property.name}}