从Django模型中获取项目并在HTML中显示。

huangapple go评论56阅读模式
英文:

Get item from Django model into HTML

问题

我想在模型models.py中的每个通知中包含标题和消息。我不想更改我所有的views.py来实现这一点。我知道可以使用标签{% request.user.name %}来获取用户的详细信息,但如何在另一个随机模型中实现呢?

我已经尝试了一些方法。以下是我尝试做出更改/创建的文件。
home是我的应用程序的名称)

home/templatetags/__init__.py

from .custom_tags import register

home/templatetags/custom_tags.py

from django import template
from home.models import Notifications

register = template.Library()

@register.simple_tag
def notifications():
    data = Notifications.objects.all()  
    return data

home/templates/base.html

{% load custom_tags %}
<html>
<head><!-- 这里有一些代码 --></head>
<body>
    <!-- 这里有更多的代码 -->
    {% notifications %}
    {% for notification in notifications_list %}
        {{ notification.title }}
    {% endfor %}
</body>

base.html中,{% notifications %}这一行显示为<QuerySet [<Notifications: Notifications object (1)>]>。但所有其他行都没有显示任何内容。

有人可以告诉我我做错了什么吗?

英文:

I want to include the title and message from every notification in the model Notifications from models.py. I don't want to have to change every single views.py that I have to do this. I know that it's possible to put the tag {% request.user.name %} to get details from the user, but how can I do this with another random model?

I have already tried some things.
These are the files that I created/changed in an attempt to do this.
(home is the name of my app)

home/templatetags/__init__.py

from .custom_tags import register

home/templatetags/custom_tags.py

from django import template
from home.models import Notifications

register = template.Library()

@register.simple_tag
def notifications():
    data = Notifications.objects.all()  
    return data

home/templates/base.html

{% load custom_tags %}
&lt;html&gt;
&lt;head&gt;&lt;!-- Some code here --&gt;&lt;/head&gt;
&lt;body&gt;
    &lt;!-- Some more code here --&gt;
    {% notifications %}
    {% for notification in notifications_list %}
        {{ notification.title }}
    {% endfor %}
&lt;/body&gt;

In base.html, the line {% notifications %} shows &lt;QuerySet [&lt;Notifications: Notifications object (1)&gt;]&gt;. But all of the other lines don't do anything.

Can anyone tell me what I'm doing wrong?

答案1

得分: 2

一个解决方案是在自定义标签内部呈现html并使用属于该标签的小型html模板将其返回
~~~
@register.simple_tag
def notifications():
    data = Notifications.objects.all()  
    context = {"data": data}
    return render_to_string("...../notification_tag.html", context)
~~~
然后在base.html中使用
~~~
<body>
    <!-- 这里还有一些其他代码 -->
    {% notifications %}
</body>
~~~
英文:

One solution is to render html inside the custom tag with a small html template belonging to the tag and return it:

@register.simple_tag
def notifications():
    data = Notifications.objects.all()  
    context = {&quot;data&quot;: data}
    return render_to_string(&quot;...../notification_tag.html&quot;, context)

in the base.html then just use:

&lt;body&gt;
    &lt;!-- Some more code here --&gt;
    {% notifications %}
&lt;/body&gt;

huangapple
  • 本文由 发表于 2023年6月15日 17:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定