【问题标题】:Django- passing object in context for view works, but how to pass to include?Django-在上下文中为视图传递对象,但如何传递给包含?
【发布时间】:2020-03-16 15:03:08
【问题描述】:

感谢反馈更新:我包含一个模板,我可以在其中访问已登录的用户,但我希望能够访问该用户的相应人员记录(由我为补充字段制作的 Profile 模型链接),所以我可以在顶部导航中显示指向他们页面的小链接。我可以在views.py中获取单个视图的Person记录,但是我的top_nav.html(模板,或laravel/rails世界中的“部分”)在不使用views.py的情况下被包含在内,所以我不确定如何获取/在没有views.py的情况下将额外的项目传递给模板(并且不想重复将人传递给包括顶部导航的每个视图)。我希望能够在用户上添加一个方法,这样我就可以像 user.get_person 一样访问它,但是扩展 auth.User 看起来就像一场噩梦。


我正在制作我的第一个 Django 应用程序,我可以在一个视图中传递/访问请求用户的变量,但不能在另一个(部分/模板)视图中传递/访问变量。

详细信息:我的顶部导航栏有一个部分,我想在其中包含一个指向已登录用户的“个人页面”的链接。当我包含该部分视图时,我什至会传递用户,尽管我不确定如何在views.py 中访问它。在views.py 中,我可以使用request.user 为person_index 视图的用户传递一个QuerySet,但这在top_nav 中不可用。当我从base.html调用top_nav时,我不确定是否/如何以某种方式从views.py中获取用户?

人物索引视图是这样设置的:

  • person_index.html 扩展 base.html
  • base.html 包含 top_nav.html
  • 那么视图的“肉”在 {% block content %}

查看下面的views.py:我为person_index(face_link部分工作)抓取了这个人,但显然我不想为每个视图获取/传递它,而是我想做一次并且能够通过它沿着 top_nav.html。

views.py:

def person_index(request):
    accessible_branches = get_valid_branches(request)
    this_person = get_user_person(request.user).first()
    person_list = Person.objects.order_by('display_name') 
    context = { 'person_list': person_list,
                'accessible_branches':accessible_branches, 'this_person': this_person}
    return render(request, 'familytree/person_index.html', context)

def get_user_person(user):
    try:
        this_user_person = Person.objects.filter(profile__user_id=user)
    except Profile.DoesNotExist:
        this_user_person = None
    return this_user_person

top_nav.html:(我希望能够获取/传递人员记录)

<div class="container">
    <div id="navbar" class="collapse navbar-collapse">
    <ul class="nav navbar-nav">

        <a href="{% url 'dashboard' %}">Home</a>
        <a href="{% url 'family_index' %}">Families</a>
        <a href="{% url 'person_index' %}">People</a>
        {%  if this_person %}
            {% include "familytree/face_link.html" with person=this_person %}
        {% endif %}
    </ul>
    </div>
</div>

base.html:(用户记录“免费”,需要获取其对应的profile__person)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>
        Our big family tree
           {% block title %}
           {{ page_title|default:"Untitled Page" }}
          {% endblock title %}
        </title>
    </head>

    <body>
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'familytree/style.css' %}">

{#    Top nav#}
    {% include "familytree/top_nav.html" with user=user %} <br/>

{#  Separator line#}
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-10">
                <hr class = "line" />
         </div>
        </div>
    </div>

{#  Page content#}
    <div class="container">
        {% block content %}
            <p>Placeholder text in base template- this gets replaced with page content.</p>
        {% endblock content %}
    </div>
    </body>
</html>

我是否缺少正确传递/获取此部分包含的用户的内容?是否有关于谁可以加载什么的规则?

【问题讨论】:

  • 你确定这条线this_user_person = Person.objects.filter(profile__user_id=user)返回什么?
  • 感谢 Wariored- 嗯...我确实对每个视图都使用相同的方法,但如果一个请求有用户而另一个请求没有用户,它可能会给出不同的响应。 . 我看看能不能确认...
  • 担心就是这样!而且我忘记了 base.html 专门针对一个用户(并且只有那个)调用了 top nav - 我的错误!
  • "但不是另一个(部分)视图。" => 如果这应该是“部分”的一些类似 rails 的定义,那么这在 django 中不存在。提示:您可能希望使用自定义模板标签以避免重复代码。
  • 模板包含不会调用视图。 include "familytree/top_nav.html" with user=user top_nav 期待 this_person,而不是 user。

标签: django


【解决方案1】:

试试这个方法

def top_nav(request):
    this_user_person = Person.objects.get(profile__user=request.user)
    context = { 'this_user_person': this_user_person}
    return render(request, 'familytree/top_nav.html', context)

在你可以调用的模板中

{{ this_user_person.first_name }}

【讨论】:

  • 谢谢,bmons!这并没有改变任何症状,但它不那么混乱,所以我会相应地更新我的代码
  • 我已经用get call更新了答案,否则试试
  • 另一种方法是您可以在任何模板中获取 request.user.profile.first_name
  • 感谢 bmons-我认为问题是在 views.py 中获取这些对象很容易,但我的 top_nav.html 被“包含”在 base.html 中,因此它不使用视图.py,所以我必须找到一些 other 方法来检索此用户的 Person 对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多