[英]How can I get my html to display my django model?
I'm trying to get my django model to be shown in the footer of my base.html but I can't get it to show up.我试图让我的 django model 显示在我的 base.html 的页脚中,但我无法让它显示出来。 I've looked at a few videos and I can't figure out where I'm going wrong.我看了一些视频,我不知道我哪里出错了。 I know that the model works as I've made 4 entries in my database and I can view them on the admin page.我知道 model 工作正常,因为我在我的数据库中创建了 4 个条目,我可以在管理页面上查看它们。 The code also doesn't show any errors so I have nothing to go off of there.该代码也没有显示任何错误,因此我对 go 没有任何了解。 Here it is:这里是:
Models.py模型.py
class SocialMediaPlatform(models.Model):
name = models.CharField(max_length=50, blank=True, null=True)
font_awesome_class = models.CharField(max_length=50, blank=True, null=True)
base_url = models.CharField(max_length=50, blank=True, null=True,
default='https://instagram.com/ or https://tiktok.com/@')
def __str__(self):
return self.base_url
Views.py视图.py
def social_media_base_view(request):
context = {}
smbase = SocialMediaPlatform.objects.all()
context['smbase'] = smbase
return render(request, 'base.html', context)
Urls.py网址.py
urlpatterns = [
path('', views.social_media_base_view),
]
Admin.py管理员.py
@admin.register(SocialMediaPlatform)
class SocialPlatformAdmin(admin.ModelAdmin):
list_display = ('name', 'font_awesome_class', 'base_url')
base.html底座.html
{% for smlink in smbase %}
<a href="{{ smlink.name }}">
<i class="{{ smlink.font_awesome_class }}">
</a>
{% endfor %}
Look what you are passing as context:查看您作为上下文传递的内容:
def social_media_base_view(request):
...
smbase = SocialMediaPlatform.objects.all()
context['smbase'] = smbase
return ...
It's QuerySet
of SocialMediaPlatform
objects.它是SocialMediaPlatform
对象的QuerySet
。 It means, that you can render them one by one with for
loop:这意味着,您可以使用for
循环一一渲染它们:
{% for smlink in smbase %}
{{ smlink.name }}
{% endfor %}
You don't need to call SocialMediaPlatform
model additionaly.您无需另外调用SocialMediaPlatform
model。
in your view you use:在您看来,您使用:
return render(request, 'index.html', context)
i can not see, if you are defined index.html
Somethere in index you should have我看不到,如果你定义index.html
索引中的某处你应该有
{% extends base.htm %}
The second, i am agree with @Shabble:第二,我同意@Shabble:
{% for smlink in smbase %}
<a href="{{ smlink.name }}">
<i class="{{ smlink.font_awesome_class }}">
</a>
{% endfor %}
The last: Try to use Django-GCBV TemplateView
.最后:尝试使用 Django-GCBV TemplateView
。 Or ListView
, in your case it is better:或ListView
,在您的情况下更好:
Class MyListView(ListView):
model = SocialMediaPlatform
template_name = index.html
urlpatterns = [
path('', MyListView.as_view()),
]
{% for smlink in object_list %}
<a href="{{ smlink.name }}">
<i class="{{ smlink.font_awesome_class }}">
</a>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.