【问题标题】:django what variables available in a template .html file?django 模板 .html 文件中有哪些变量可用?
【发布时间】:2017-08-12 18:29:51
【问题描述】:

我正在尝试找出模板 .html 文件中可用的对象和变量

这是在视图文件中

from django.contrib.auth import get_user_model
Userx = get_user_model()

class ProfileDetailView(DetailView):
    template_name ='profiles/user.html'

    def get_object(self):
        username = self.kwargs.get("username")
        if username is None:
            raise Http404
        return get_object_or_404(Userx,username__iexact=username, is_active=True)

在user.html文件中

{% block content %}
username={{ user.username }}
{% endblock content %}

我的问题 1.为什么user.html文件中的对象名叫user?

  1. 如何找到有关用户对象的对象名称和字段的文档?

  2. 如何查看特定 .html 模板中可访问的所有可用变量”

感谢您的帮助

【问题讨论】:

    标签: html django django-templates


    【解决方案1】:
    1. 这是为引用用户对象而定义的名称(很难期待别的东西)

    2. 在这里你可以找到这个User object的每个字段

    3. 您可以使用{% debug %} 来检查上下文和很多其他信息,而这个link 可以帮助您找到要在模板中使用的特定关键字。

      还要检查this post,尤其是以下行(有助于检查调试输出):

       {% filter force_escape %} {% debug %} {% endfilter %} 

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-03
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2017-10-01
      • 2022-11-03
      • 2020-04-17
      相关资源
      最近更新 更多