Django框架:对象不显示在特定pk的网页上

时间:2018-05-11 13:02:07

标签: python django django-rest-framework

我正在尝试使用Django Framework将特定项目从数据库导入到网页中。当我拉所有记录时,我设法做到了这一点,但当我尝试按主键选择单个记录时却没有。

这是显示页面和数据库字段的html:

{% extends "base.html" %}
{% load static %}
{% block content %}
    <br>
    <h3>Adherence agreement for authorized access to data and biospecimens in the RD-Connect Genome-Phenome Analysis Platform (RDC-GPAP)<sup><a href="#fn1" id="ref1">1</a></sup><small class="text-muted"> to be used together with the <span class="text-info">Code of Conduct</span> for integrated user access to RDC-GPAP for health-related information and human biological samples.</small></h3>
    <hr>

    {% for item in items %}

    <p>I, <span class="text-success">{{ item.first_name }} {{ item.last_name }}</span>, acting on behalf of the <span class="text-success">{{ item.department }}</span>, and holding the position <span class="text-success">{{ item.job_title }}</span>, herewith declare:</p>
    <ol type="1">
        <li>I have the authority to represent and engage the <span class="text-success">{{ item.department }}</span> for the purpose of the project, <span class="text-success">[project name]</span>. For this project we ask for access to data and/or biospecimens in accordance with our research interests as set out in our completed <span class="text-info">User Verification Form</span> (previous page).</li>
        <br>
        <li>The <span class="text-success">{{ item.department }}</span> agrees to fully endorse and adhere to the <span class="text-info">Code of Conduct</span> for integrated user access to the RDC-GPAP for health-related information and human biological samples, version 2, 3rd November 2017. It shall apply to all data and biospecimen processing activities carried out within the project <span class="text-success">[project name]</span>. The personal data protection framework is thus in part formalized through this Code.</li>
        <br>
        <li>The <span class="text-success">{{ item.department }}</span> will ensure the implementation of all measures required by the provisions of this Code.</li>
        <br>
        <li>The <span class="text-success">{{ item.department }}</span> will ensure compliance with this Code by all staff and personnel working within the project on behalf of the <span class="text-success">{{ item.department }}</span>.</li>
        <br>
        <li>In addition to the rules laid out by the Code, the following project specific rules shall apply:</li>
        <br>
        <ol type="a">
            <li>There will be no attempt to try to identify or contact data or donor subjects.</li>
            <li>Accessed data and biospecimens will not be redistributed.</li>
            <li>Access codes and user logins are specific to the identified user and are strictly non-transferable.</li>
            <li>Accessed datasets will be destroyed once they are no longer used.</li>
            <li>Biospecimens will be handled in accordance with specifications in a Material Transfer Agreement.</li>
            <li>Publications using any form of data accessed through the RDC-GPAP will contain an acknowledgment and a reference of the RDC-GPAP as appropriate.
            Where a publication makes use of individual-level datasets and enriched phenotypic data, authors should consider whether the intellectual contribution of the original contributors of the datasets qualifies for authorship positions in line with standard publication practice.</li>
            <li>Results of analyses of accessed data and biospecimens will be returned to the platform. Published work will also be sent to RD-Connect.</li>
            <li>In case of misconduct access to the platform will be withdrawn.</li>
        </ol>
    </ol>
    <hr>
    <p>Signed on behalf of the <span class="text-success">{{ item.department }}</span> on <span class="text-success">{% now "jS F Y" %}</span> by <span class="text-success">{{ item.first_name }} {{ item.last_name }}</span>:</p>

    {% endfor %}

    <br>
    <br>
    <p>…………………………………………………</p>
    <p>Signature</p>
    <hr>
    <sup id="fn1">1. This adherence agreement is based on the Personal Data Directive, Directive95/46/EC, implying that all personal data shall be handled in accordance with Secrecy law.<a href="#ref1" title="Jump back to footnote 1 in the text.">↩</a></sup>
    <br>
    <br>
</div>
<div class="container">
    <input type="button" class="save btn btn-success btn-lg" onclick="printDiv('printable')" value="Print" />
    <br>
</div>
{% endblock %}

urls.py:

urlpatterns = [
    path('adherence_agreement/', views.adherence_agreement, name='adherence_agreement'),
]

然后,当我在views.py中使用以下代码时,我会通过使用data重复{% for item in items %}并选择单个字段来获取网页中的相关字段。 {{ item.first_name }}

def adherence_agreement(request):
    try:
        data = UserInfo.objects.all()
    except UserInfo.DoesNotExist:
        raise Http404('User does not exist')
    return render(
        request,
        'adherence_agreement.html', {
            'items': data
        }
    )

但是,如果我想通过将urlpattern更改为path('adherence_agreement/<int:id>'...并更改views.py来提取特定记录的信息,请执行以下操作:

def adherence_agreement(request,id):
    try:
        data = UserInfo.objects.get(pk=id)
    except UserInfo.DoesNotExist:
        raise Http404('User does not exist')
    datum = data._meta.get_fields()
    return render(
        request,
        'adherence_agreement.html', {
            'items': datum
        }
    )

然后网页加载(没有错误),但没有任何字段被拉过......

仅供参考,models.py的{​​{1}}:

UserInfo

2 个答案:

答案 0 :(得分:5)

我不明白为什么你在视图中做datum = data._meta.get_fields()。这将获得您根本不想要的类级别字段对象。不要那样做;将对象直接传递给模板。

您还需要删除模板中的循环,并将对象直接传递为item

作为进一步的优化,您可以使用get_object_or_404而不是捕获DoesNotExist异常并手动提升404。

def adherence_agreement(request,id):
    item = get_object_or_404(UserInfo, pk=id)
    return render(request, 'adherence_agreement.html', {'item': item})

答案 1 :(得分:1)

您的模板期望可迭代,并且您正在检索单个项目,因此您可以将其放入列表中以使其可迭代。您还可以使用get_object_or_404(如果未找到则抛​​出404)来减少代码。

def adherence_agreement(request, id):
    data = get_object_or_404(UserInfo, pk=id)
    return render(
        request,
        'adherence_agreement.html', 
        {
            'items': [data]
        }
    )