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

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

Get item from Django model into HTML

问题

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

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

home/templatetags/__init__.py

  1. from .custom_tags import register

home/templatetags/custom_tags.py

  1. from django import template
  2. from home.models import Notifications
  3. register = template.Library()
  4. @register.simple_tag
  5. def notifications():
  6. data = Notifications.objects.all()
  7. return data

home/templates/base.html

  1. {% load custom_tags %}
  2. <html>
  3. <head><!-- 这里有一些代码 --></head>
  4. <body>
  5. <!-- 这里有更多的代码 -->
  6. {% notifications %}
  7. {% for notification in notifications_list %}
  8. {{ notification.title }}
  9. {% endfor %}
  10. </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

  1. from .custom_tags import register

home/templatetags/custom_tags.py

  1. from django import template
  2. from home.models import Notifications
  3. register = template.Library()
  4. @register.simple_tag
  5. def notifications():
  6. data = Notifications.objects.all()
  7. return data

home/templates/base.html

  1. {% load custom_tags %}
  2. &lt;html&gt;
  3. &lt;head&gt;&lt;!-- Some code here --&gt;&lt;/head&gt;
  4. &lt;body&gt;
  5. &lt;!-- Some more code here --&gt;
  6. {% notifications %}
  7. {% for notification in notifications_list %}
  8. {{ notification.title }}
  9. {% endfor %}
  10. &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

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

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

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

in the base.html then just use:

  1. &lt;body&gt;
  2. &lt;!-- Some more code here --&gt;
  3. {% notifications %}
  4. &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:

确定