【问题标题】:Django-- Get model fields in template only for current userDjango--仅为当前用户获取模板中的模型字段
【发布时间】:2017-11-11 21:40:23
【问题描述】:

首先,我知道有很多关于在模板中访问模型的问题,但让我解释一下为什么会有所不同。

我想要用户可以查看其详细信息的个人资料页面。我能够做到这一点,但有一个小错误。如果有 3 个用户(比如 A、B、C)并且用户 A 想查看他的个人资料,他会看到 3 次。像这样:

如何在一次迭代后停止循环,以便用户只获得一次他的个人资料信息。

这是我的网址:

url(r'^profile/$', views.IndexView.as_view(), name='profile'),

Views.py:

class IndexView(generic.ListView):
template_name = 'profile.html'

def get_queryset(self):
    return Profile.objects.all()

和profile.html:

{% if user.is_authenticated %}
{% for profile in object_list %}
<h1>{{ request.user }}'s Profile</h1>
<table>
<tr>
    <td>Username:</td>
    <td>{{ user.username }}</td>
</tr>
<tr>
    <td>First Name:</td>
    <td>{{ user.first_name }}</td>
</tr>
<tr>
    <td>Last Name:</td>
    <td>{{ user.last_name }}</td>
</tr>
<tr>
    <td>Email:</td>
    <td>{{ user.email }}</td>
</tr>
<tr>
    <td>Personal Info:</td>
    <td>{{ user.profile.personal_info }}</td>
</tr>
<tr>
    <td>Job Title:</td>
    <td>{{ user.profile.job_title }}</td>
</tr>
<tr>
    <td>Department:</td>
    <td>{{ user.profile.department }}</td>
</tr>
<tr>
    <td>Location:</td>
    <td>{{ user.profile.location }}</td>
</tr>
<tr>
    <td>Expertise:</td>
    <td>{{ user.profile.expertise }}</td>
</tr>
<tr>
    <td>Phone Number:</td>
    <td>{{ user.profile.phone_number }}</td>
</tr>
<tr>
    <td>Contact Skype:</td>
    <td>{{ user.contact_skype }}</td>
</tr>
<tr>
    <td>Contact Facebook:</td>
    <td>{{ user.contact_facebook }}</td>
</tr>
<tr>
    <td>Contact Linkedin:</td>
    <td>{{ user.profile.contact_linkedin }}</td>
</tr>
</table>

<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {% include 'form-template.html' %}
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <a href={%  url 'profile_edit' %}><input type="button" class = " col-sm-offset-2 btn bk-bs-btn-warning " name="cancel" value="Edit" /></a>
            </div>
        </div>
</form>
{% endfor %}
{% else %}
    <h2>Please login to see your Profile</h2>
{% endif %}

我是 django 的新手,先谢谢了。

【问题讨论】:

    标签: django django-models django-templates django-views


    【解决方案1】:

    您正在循环所有配置文件,但实际上并未在循环中使用此数据,而是使用user.profile

    这意味着您可能在数据库中有 3 个 Profile 对象,然后为每个对象显示当前用户的详细信息,这是不可取的。

    因此,您可以完全删除循环,只保留引用所有user.profile 属性的内容即可达到预期结果。


    编辑

    看起来user 默认被传递到您的模板中,并指向当前登录的用户。所以user.profile 将返回配置文件,这无需执行任何其他操作。所以user.profile.personal_info 是有效的并且应该可以工作。当您尝试直接使用profile 时,这不是一回事,并且没有定义,所以profile.personal_info 不起作用。然后在您的循环中,您使用 profile 来循环 Profile 对象,但这不是必需的或已被使用。希望这是有道理的。

    【讨论】:

    • 谢谢。首先我没有循环但不包括用户(所以我写了 profile.personal_info 例如)它没有返回任何东西。我认为这是因为它无法识别配置文件,所以我创建了带有变量配置文件的循环。但是现在如果我写 user.profile.personal_info(例如)它可以工作。你能给我解释一下吗?
    • 添加了解释。
    猜你喜欢
    • 2019-03-23
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2012-06-15
    • 1970-01-01
    • 2012-11-16
    相关资源
    最近更新 更多