在模板中从视图文件生成的字典中迭代一个列表。

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

Iterate over a list in a template from a dictionary generated in View file

问题

以下是您提供的代码的翻译部分:

我正在使用Django创建一个表格其中包含静态和动态数据的列动态数据可以从数据库中轻松调用并循环遍历静态数据已存储在视图中格式为字典我面临的问题是当我需要在模板中迭代通过字典传递的列表时以下是视图代码

class NaturalAminoAcidInfoView(View):
    template_name = "bacteria/naturalAAInfoList.html"

    def get(self, request):

        # 创建一个包含常见细菌的列表,这些细菌属于次要、中等和广泛的定植类别
        bacteria = Bacteria.objects.all()
        keylist = ['Minor', 'Moderate', 'Widespread']
        q_objects = reduce(or_, [Q(coloniser__contains=mykey) for mykey in keylist])

        common = bacteria.filter(q_objects).filter(gm_compilations__isnull=False)
        common_count = common.count()

        amino_acid_dict = {
            "Alanine": {
                "text": "非必需氨基酸。在肽酶水解后,通过小肠刷状缘细胞吸收,至少通过6个转运体。",
                "image": "/media/image/naturalaa-info/alanine.svg",
                "use": "alanine_use",
            },
            # ... 其他氨基酸的数据
        }

        amino_acids = [
            ('Alanine', amino_acid_dict['Alanine']['use'], amino_acid_dict['Alanine']['text'], amino_acid_dict['Alanine']['image']),
            # ... 其他氨基酸的数据
        ]

        # ... 其他视图代码

        context = {
            'bacteria': bacteria,
            'important_coloniser': common_count,
            'amino_acid_counts': amino_acid_counts,
            'amino_acids': amino_acids,
            'total_count': total_count,
            'common': common,
            'example_list': example_list,
        }

        return render(request, self.template_name, context)
以下是模板代码:

<thead>
        <tr>
            <th>Amino acid</th>
            <th>Common</th>
            <th>Example species</th>
            <th>All consumers</th>
            <th>Text</th>
            <th>Image</th>
        </tr>
    </thead>
    <tbody>
        {% for amino_acid, use, text, image in amino_acids %}

        <tr>
            <td>{{ amino_acid }}</td>
            <td> {% for bug in example_list %}
                <i><a href="url 'bacteria-detail' bug.slug" target="_blank">{{bug}};</a></i>
                {% endfor %}
            <td>
                {{ amino_acid_counts|dict_lookup:amino_acid|default_if_none:''|dict_lookup:'example_list' }}
            </td>
            <td>
                {{ amino_acid_counts|dict_lookup:amino_acid|default_if_none:''|dict_lookup:'total_count' }}
            </td>
            <td>
                {{ text }}
            </td>
            <td><img src="{{ image }}" style="width: 200px; height: 150px;">
            </td>
        </tr>
        {% endfor %}
    </tbody>

在这里,我已经将您提供的Python代码和HTML模板翻译成中文。如果您有任何其他问题或需要进一步的帮助,请随时提问。

英文:

I am creating a table in Django with columns of static and dynamic data. The dynamic data is easy to call from the database and loop through. The static data has been stored in the View as a dictionary. The problem I'm facing is when I need to iterate over a list in the template that comes via the dictionary. Here is the view code:

class NaturalAminoAcidInfoView(View):
template_name = &quot;bacteria/naturalAAInfoList.html&quot;
def get(self, request):
# Creates a list of common bacteria in the minor, moderate, and widespread coloniser categories
bacteria = Bacteria.objects.all()
keylist = [&#39;Minor&#39;, &#39;Moderate&#39;, &#39;Widespread&#39;]
q_objects = reduce(or_, [Q(coloniser__contains=mykey) for mykey in keylist])
common = bacteria.filter(q_objects).filter(gm_compilations__isnull=False)   
common_count = common.count()
amino_acid_dict = {
&quot;Alanine&quot;: {
&quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via at least 6 transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/alanine.svg&quot;,
&quot;use&quot;: &quot;alanine_use&quot;,
},
&quot;Arginine&quot;: {
&quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium- and chloride-dependent symport ATB⁰,⁺ transporters, uniport CAT-1 transporters, and the high affinity antiport rBAT/b⁰,⁺AT (in exchange for a neutral AA) transporter.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/arginine.svg&quot;,
&quot;use&quot;: &quot;arginine_use&quot;
},
&quot;L-Asparagine&quot;: {
&quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via symport transporters (sodium-dependent SNAT2, sodium- and proton-dependent SNAT3, and sodium-dependent B⁰AT1).&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/l_asparagine.svg&quot;,
&quot;use&quot;: &quot;l_asparagine_use&quot;
},
&quot;Aspartate&quot;: {
&quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium(sym)-, proton(sym)- and potassium(anti)-dependent symport EAAT3 transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/aspartate.svg&quot;,
&quot;use&quot;: &quot;aspartate_use&quot;
},
&quot;Cysteine&quot;: {
&quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2 transporters, cystine-glutamate antiporter; alanine, serine, cysteine-preferring transporter 2 (ASCT2) and antiport 4F2hc/y⁺LAT2. It is also transported via antiports rBAT/b⁰,⁺AT and 4F2 hc/xCT (as cystine).&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/cysteine.svg&quot;,
&quot;use&quot;: &quot;cysteine_use&quot;
},
&quot;Glutamate&quot;: {
&quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium(sym)-, proton(sym)- and potassium(anti)-dependent symport EAAT3 and antiport 4F2 hc/xCT transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/glutamate.svg&quot;,
&quot;use&quot;: &quot;glutamate_use&quot;
},
&quot;L-Glutamine&quot;: {
&quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the sodium-dependent antiport ASCT2 transporter. Can also be transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/l_glutamine.svg&quot;,
&quot;use&quot;: &quot;l_glutamine_use&quot;
},
&quot;Glycine&quot;: {
&quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via proton-dependent symport PAT1 &amp; PAT2 transporters. Can also be transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/glycine.svg&quot;,
&quot;use&quot;: &quot;glycine_use&quot;
},
&quot;Histidine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2 transporters and the uniport CAT-1 transporters. Possibly transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/histidine.svg&quot;,
&quot;use&quot;: &quot;histidine_use&quot;
},
&quot;Isoleucine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport 4F2hc/LAT1 and uniport LAT4 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/isoleucine.svg&quot;,
&quot;use&quot;: &quot;isoleucine_use&quot;
},
&quot;Leucine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport 4F2hc/LAT1 and uniport LAT4 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/leucine.svg&quot;,
&quot;use&quot;: &quot;leucine_use&quot;
},
&quot;Lysine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium- and chloride-dependent symport ATB⁰,⁺, antiport rBAT/b⁰,⁺AT (in exchange for a neutral AA) and uniport CAT-1 transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/lysine.svg&quot;,
&quot;use&quot;: &quot;lysine_use&quot;
},
&quot;Methionine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2, antiport rBAT/b⁰,⁺AT and uniport CAT-1 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/methionine.svg&quot;,
&quot;use&quot;: &quot;methionine_use&quot;
},
&quot;Phenylalanine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport rBAT/b⁰,⁺AT and uniport CAT-1 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/phenylalanine.svg&quot;,
&quot;use&quot;: &quot;phenylalanine_use&quot;
},
&quot;Proline&quot;: {
&quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2, proton-dependent PAT1 &amp; PAT2 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/proline.svg&quot;,
&quot;use&quot;: &quot;proline_use&quot;
},
&quot;Serine&quot;: {
&quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the sodium-dependent transporters symport SNAT2 and antiport ASCT2, as well as the antiport rBAT/b⁰,⁺AT transporter.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/serine.svg&quot;,
&quot;use&quot;: &quot;serine_use&quot;
},
&quot;Threonine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport transporters ASCT2 and rBAT/b⁰,⁺AT.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/threonine.svg&quot;,
&quot;use&quot;: &quot;threonine_use&quot;
},
&quot;Tryptophan&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the antiport 4F2hc/LAT1 transporter, the uniport TAT1 transporter, or the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/tryptophan.svg&quot;,
&quot;use&quot;: &quot;tryptophan_use&quot;
},
&quot;Tyrosine&quot;: {
&quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the antiport 4F2hc/LAT1 transporter.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/tyrosine.svg&quot;,
&quot;use&quot;: &quot;tyrosine_use&quot;
},
&quot;Valine&quot;: {
&quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the antiport 4F2hc/LAT1 transporter and the neutral AA B⁰AT1 and ATB⁰,⁺ transporters.&quot;,
&quot;image&quot;: &quot;/media/image/naturalaa-info/valine.svg&quot;,
&quot;use&quot;: &quot;valine_use&quot;
}
}  
amino_acids = [
(&#39;Alanine&#39;, amino_acid_dict[&#39;Alanine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Alanine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Alanine&#39;][&#39;image&#39;]),
(&#39;Arginine&#39;, amino_acid_dict[&#39;Arginine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Arginine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Arginine&#39;][&#39;image&#39;]),
(&#39;L-Asparagine&#39;, amino_acid_dict[&#39;L-Asparagine&#39;][&#39;use&#39;], amino_acid_dict[&#39;L-Asparagine&#39;][&#39;text&#39;], amino_acid_dict[&#39;L-Asparagine&#39;][&#39;image&#39;]),
(&#39;Aspartate&#39;, amino_acid_dict[&#39;Aspartate&#39;][&#39;use&#39;], amino_acid_dict[&#39;Aspartate&#39;][&#39;text&#39;], amino_acid_dict[&#39;Aspartate&#39;][&#39;image&#39;]),
(&#39;Cysteine&#39;, amino_acid_dict[&#39;Cysteine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Cysteine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Cysteine&#39;][&#39;image&#39;]),
(&#39;Glutamate&#39;, amino_acid_dict[&#39;Glutamate&#39;][&#39;use&#39;], amino_acid_dict[&#39;Glutamate&#39;][&#39;text&#39;], amino_acid_dict[&#39;Glutamate&#39;][&#39;image&#39;]),
(&#39;L-Glutamine&#39;, amino_acid_dict[&#39;L-Glutamine&#39;][&#39;use&#39;], amino_acid_dict[&#39;L-Glutamine&#39;][&#39;text&#39;], amino_acid_dict[&#39;L-Glutamine&#39;][&#39;image&#39;]),
(&#39;Glycine&#39;, amino_acid_dict[&#39;Glycine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Glycine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Glycine&#39;][&#39;image&#39;]),
(&#39;Histidine&#39;, amino_acid_dict[&#39;Histidine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Histidine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Histidine&#39;][&#39;image&#39;]),
(&#39;Isoleucine&#39;, amino_acid_dict[&#39;Isoleucine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Isoleucine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Isoleucine&#39;][&#39;image&#39;]),
(&#39;Leucine&#39;, amino_acid_dict[&#39;Leucine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Leucine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Leucine&#39;][&#39;image&#39;]),
(&#39;Lysine&#39;, amino_acid_dict[&#39;Lysine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Lysine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Lysine&#39;][&#39;image&#39;]),
(&#39;Methionine&#39;, amino_acid_dict[&#39;Methionine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Methionine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Methionine&#39;][&#39;image&#39;]),
(&#39;Phenylalanine&#39;, amino_acid_dict[&#39;Phenylalanine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Phenylalanine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Phenylalanine&#39;][&#39;image&#39;]),
(&#39;Proline&#39;, amino_acid_dict[&#39;Proline&#39;][&#39;use&#39;], amino_acid_dict[&#39;Proline&#39;][&#39;text&#39;], amino_acid_dict[&#39;Proline&#39;][&#39;image&#39;]),
(&#39;Serine&#39;, amino_acid_dict[&#39;Serine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Serine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Serine&#39;][&#39;image&#39;]),
(&#39;Threonine&#39;, amino_acid_dict[&#39;Threonine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Threonine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Threonine&#39;][&#39;image&#39;]),
(&#39;Tryptophan&#39;, amino_acid_dict[&#39;Tryptophan&#39;][&#39;use&#39;], amino_acid_dict[&#39;Tryptophan&#39;][&#39;text&#39;], amino_acid_dict[&#39;Tryptophan&#39;][&#39;image&#39;]),
(&#39;Tyrosine&#39;, amino_acid_dict[&#39;Tyrosine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Tyrosine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Tyrosine&#39;][&#39;image&#39;]),
(&#39;Valine&#39;, amino_acid_dict[&#39;Valine&#39;][&#39;use&#39;], amino_acid_dict[&#39;Valine&#39;][&#39;text&#39;], amino_acid_dict[&#39;Valine&#39;][&#39;image&#39;]),
]
key2 = list(amino_acid_dict.keys())
key = [amino_acid_dict[k][&quot;use&quot;] for k in key2]
def process_data(field_name):
consumer = common.filter(**{field_name + &#39;__contains&#39;: &#39;+&#39;})
bugs = []
for bug in consumer:
bugs.append(bug.species)
return bugs
for k in key:
example_list = process_data(k)[:5]
amino_acid_counts = {}
for amino_acid, field_name, text, image in amino_acids:
consumers = common.filter(**{field_name + &#39;__contains&#39;: &#39;+&#39;})
common_consumers = consumers.filter(gm_compilations__isnull=False).annotate(compilation_count=Count(&#39;gm_compilations&#39;, distinct=True))
common_consumers_count = common_consumers.count()
amino_acid_count = consumers.count()
total_count = Bacteria.objects.filter(**{field_name + &#39;__contains&#39;: &#39;+&#39;}).count()
most_common_bugs = []
most_common = [bug for bug in common_consumers if bug.gm_compilations and (bug.gm_compilations.count(&#39;,&#39;) + 1) &gt;= 1]
for bug in most_common:
most_common_bugs.append(bug.species)
most_common_bugs_examples = set(most_common_bugs[:5])
amino_acid_counts[amino_acid] = {
&#39;common_consumers_count&#39;: common_consumers_count,
&#39;count&#39;: amino_acid_count,
&#39;total_count&#39;: total_count,
&#39;most_common&#39;: most_common,
&#39;most_common_bugs&#39;: most_common_bugs,
&#39;most_common_bugs_examples&#39;: most_common_bugs_examples,
&#39;text&#39;: text,
&#39;image&#39;: image,
}
context = {
&#39;bacteria&#39;: bacteria,
&#39;important_coloniser&#39;: common_count,
&#39;amino_acid_counts&#39;: amino_acid_counts,
&#39;amino_acids&#39;: amino_acids,
&#39;total_count&#39;: total_count,
&#39;common&#39;: common,
&#39;example_list&#39;: example_list,
}
return render(request, self.template_name, context)

Here is the template code:

&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Amino acid&lt;/th&gt;
&lt;th&gt;Common&lt;/th&gt;
&lt;th&gt;Example species&lt;/th&gt;
&lt;th&gt;All consumers&lt;/th&gt;
&lt;th&gt;Text&lt;/th&gt;
&lt;th&gt;Image&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{% for amino_acid, use, text, image in amino_acids %}
&lt;tr&gt;
&lt;td&gt;{{ amino_acid }}&lt;/td&gt;
&lt;td&gt; {% for bug in example_list %}
&lt;i&gt;&lt;a href=&quot;url &#39;bacteria-detail&#39; bug.slug&quot; target=&quot;_blank&quot;&gt;{{bug}};&lt;/a&gt;&lt;/i&gt;
{% endfor %}
&lt;td&gt;
{{ amino_acid_counts|dict_lookup:amino_acid|default_if_none:&#39;&#39;|dict_lookup:&#39;example_list&#39; }}
&lt;/td&gt;
&lt;td&gt;
{{ amino_acid_counts|dict_lookup:amino_acid|default_if_none:&#39;&#39;|dict_lookup:&#39;total_count&#39; }}
&lt;/td&gt;
&lt;td&gt;
{{ text }}
&lt;/td&gt;
&lt;td&gt;&lt;img src=&quot;{{ image }}&quot; style=&quot;width: 200px; height: 150px;&quot;&gt;
&lt;/td&gt;
&lt;/tr&gt;
{% endfor %}
&lt;/tbody&gt;

This works fine, except for {{ amino_acid_counts|dict_lookup:amino_acid|default_if_none:&#39;&#39;|dict_lookup:&#39;example_list&#39; }} which just displays the list like this: ['bacteria1', 'bacteria2' etc.]. Ideally, I'd like to iterate over this list and link urls to each list item, e.g.:

                {% for bug in example_list.items %}
&lt;i&gt;&lt;a href=&quot;/bacteria/{{ bug.slug }}&quot; target=&quot;_blank&quot;&gt;{{bug}};&lt;/a&gt;&lt;/i&gt;
{% endfor %}

Is there a way to do this either in the template or in the view?

答案1

得分: 0

你可以使用named URL和{% url %}模板标签。

&lt;i&gt;
{% for bug in example_list.items %}
&lt;a href=&quot;{% url &#39;my_app:bacteria&#39; bug.slug %}&quot; target=&quot;_blank&quot;&gt;{{ bug }};&lt;/a&gt;&amp;nbsp;
{% endfor %}
&lt;/i&gt;

在你的urls.py文件中,你应该有一个类似这样的命名URL:

from django.urls import path
from . import views
app_name = &quot;my_app&quot;
urlpatterns = [
...
path(&quot;bacteria/&lt;slug:slug&gt;/&quot;, views.bacteria_detail, name=&quot;bacteria&quot;),
...
]

附加资源:

英文:

You can use the named URL and the {% url %} template tag.

&lt;i&gt;
{% for bug in example_list.items %}
&lt;a href=&quot;{% url &#39;my_app:bacteria&#39; bug.slug %}&quot; target=&quot;_blank&quot;&gt;{{ bug }};&lt;/a&gt;&amp;nbsp;
{% endfor %}
&lt;/i&gt;

In your urls.py, you should have a named URL like this :

from django.urls import path
from . import views
app_name = &quot;my_app&quot;
urlpatterns = [
...
path(&quot;bacteria/&lt;slug:slug&gt;/&quot;, views.bacteria_detail, name=&quot;bacteria&quot;),
...
]

additional resources:

答案2

得分: 0

以下是您提供的代码的翻译部分:

# 新修改的代码(如下)解决了我遇到的所有问题。这将是一个有用的通用模板,我可以用来创建许多其他表格,我只需要更改底物的简单列表和每个底物的任何特定文本。以下是视图代码:

class NaturalAminoAcidInfoView(View):
    template_name = "bacteria/naturalAAInfoList.html"

    def get(self, request):

        # 创建常见细菌的分类列表:轻微,中等和广泛的定居者
        bacteria = Bacteria.objects.all()
        keylist = ['轻微', '中等', '广泛']
        q_objects = reduce(or_, [Q(coloniser__contains=mykey) for mykey in keylist])

        common = bacteria.filter(q_objects).filter(gm_compilations__isnull=False)
        common_count = common.count()

        def generate_bacterium_list(amino_acid):
            consumers = common.filter(**{amino_acid.lower() + '_use__contains': '+'})
            bacterium_list = []
            for bacteria in consumers:
                bacterium_list.append(bacteria)
            return bacterium_list

        amino_acid_dict = {}

        text_dict = {
            "丙氨酸": {
                "text": "非必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,至少通过6种转运体。",
            },
            "精氨酸": {
                "text": "有条件必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过钠和氯依赖的共同转运体ATB⁰,⁺、单独转运的CAT-1转运体,以及高亲和力的rBAT/b⁰,⁺AT(以换取中性氨基酸)转运体。",
            },
            "L-天冬酰胺": {
                "text": "非必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过共同转运体(钠依赖的SNAT2、钠和质子依赖的SNAT3,以及钠依赖的B⁰AT1)。",
            },
            "天冬氨酸": {
                "text": "非必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过钠(共同)、质子(共同)和钾(反向)依赖的共同转运体EAAT3转运体。",
            },
            "半胱氨酸": {
                "text": "有条件必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过钠依赖的共同转运体SNAT2转运体、半胱氨酸-谷氨酸抗移物体;丙氨酸、丝氨酸、半胱氨酸优先转运体2(ASCT2)和反向转运体4F2hc/y⁺LAT2。它还可通过反向转运体rBAT/b⁰,⁺AT和4F2 hc/xCT(作为半胱氨酸)转运。",
            },
            "谷氨酸": {
                "text": "非必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过钠(共同)、质子(共同)和钾(反向)依赖的共同转运体EAAT3和反向转运体4F2 hc/xCT转运体。",
            },
            "L-谷氨酰胺": {
                "text": "有条件必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过钠依赖的反向转运体ASCT2转运体。也可以与中性氨基酸B⁰AT1和ATB⁰,⁺转运体一起运输。",
            },
            "甘氨酸": {
                "text": "有条件必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过质子依赖的共同转运体PAT1和PAT2转运体。也可以与中性氨基酸B⁰AT1和ATB⁰,⁺转运体一起运输。",
            },
            "组氨酸": {
                "text": "必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过钠依赖的共同转运体SNAT2转运体和单独转运的CAT-1转运体。可能也与中性氨基酸B⁰AT1和ATB⁰,⁺转运体一起运输。",
            },
            "异亮氨酸": {
                "text": "必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过反向转运体4F2hc/LAT1和单独转运的LAT4转运体。也可能与中性氨基酸B⁰AT1和ATB⁰,⁺转运体一起运输。",
            },
            "亮氨酸": {
                "text": "必需氨基酸。经肽酶水解后,通过小肠刷状缘细胞吸收,通过反向转运体4F2hc/LAT1和单独转运的

<details>
<summary>英文:</summary>

The new modified code (below) solves all the issues I was having. It will be a useful generic template I can use for many other tables I need to create, where I&#39;m only having to change a simple list of substrates and any specific text for each substrate. Here is the View code:

    class NaturalAminoAcidInfoView(View):
    template_name = &quot;bacteria/naturalAAInfoList.html&quot;

    def get(self, request):

        # Creates a list of common bacteria in the minor, moderate, and widespread coloniser categories
        bacteria = Bacteria.objects.all()
        keylist = [&#39;Minor&#39;, &#39;Moderate&#39;, &#39;Widespread&#39;]
        q_objects = reduce(or_, [Q(coloniser__contains=mykey) for mykey in keylist])

        common = bacteria.filter(q_objects).filter(gm_compilations__isnull=False)   
        common_count = common.count()

        def generate_bacterium_list(amino_acid):
            consumers = common.filter(**{amino_acid.lower() + &#39;_use__contains&#39;: &#39;+&#39;})
            bacterium_list = []
            for bacteria in consumers:
                bacterium_list.append(bacteria)
            return bacterium_list
                
        amino_acid_dict = {}

        text_dict = {
            &quot;Alanine&quot;: {
                &quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via at least 6 transporters.&quot;,
            },
            &quot;Arginine&quot;: {
                &quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium- and chloride-dependent symport ATB⁰, transporters, uniport CAT-1 transporters, and the high affinity antiport rBAT/b⁰,AT (in exchange for a neutral AA) transporter.&quot;,
            },
            &quot;L_Asparagine&quot;: {
                &quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via symport transporters (sodium-dependent SNAT2, sodium- and proton-dependent SNAT3, and sodium-dependent B⁰AT1).&quot;,
            },
            &quot;Aspartate&quot;: {
                &quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium(sym)-, proton(sym)- and potassium(anti)-dependent symport EAAT3 transporters.&quot;,
            },
            &quot;Cysteine&quot;: {
                &quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2 transporters, cystine-glutamate antiporter; alanine, serine, cysteine-preferring transporter 2 (ASCT2) and antiport 4F2hc/yLAT2. It is also transported via antiports rBAT/b⁰,AT and 4F2 hc/xCT (as cystine).&quot;,
            },
            &quot;Glutamate&quot;: {
                &quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium(sym)-, proton(sym)- and potassium(anti)-dependent symport EAAT3 and antiport 4F2 hc/xCT transporters.&quot;,
            },
            &quot;L_Glutamine&quot;: {
                &quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the sodium-dependent antiport ASCT2 transporter. Can also be transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Glycine&quot;: {
                &quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via proton-dependent symport PAT1 &amp; PAT2 transporters. Can also be transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Histidine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2 transporters and the uniport CAT-1 transporters. Possibly transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Isoleucine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport 4F2hc/LAT1 and uniport LAT4 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Leucine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport 4F2hc/LAT1 and uniport LAT4 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Lysine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium- and chloride-dependent symport ATB⁰,, antiport rBAT/b⁰,AT (in exchange for a neutral AA) and uniport CAT-1 transporters.&quot;,
            },
            &quot;Methionine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2, antiport rBAT/b⁰,AT and uniport CAT-1 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Phenylalanine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport rBAT/b⁰,AT and uniport CAT-1 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Proline&quot;: {
                &quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via sodium-dependent symport SNAT2, proton-dependent PAT1 &amp; PAT2 transporters. Can probably also be transported with the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Serine&quot;: {
                &quot;text&quot;: &quot;A non-essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the sodium-dependent transporters symport SNAT2 and antiport ASCT2, as well as the antiport rBAT/b⁰,AT transporter.&quot;,
            },
            &quot;Threonine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via antiport transporters ASCT2 and rBAT/b⁰,AT.&quot;,
            },
            &quot;Tryptophan&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the antiport 4F2hc/LAT1 transporter, the uniport TAT1 transporter, or the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            },
            &quot;Tyrosine&quot;: {
                &quot;text&quot;: &quot;A conditionally essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the antiport 4F2hc/LAT1 transporter.&quot;,
            },
            &quot;Valine&quot;: {
                &quot;text&quot;: &quot;An essential AA. After peptidase hydrolysis, it is absorbed through brush border cells of the small intestine, via the antiport 4F2hc/LAT1 transporter and the neutral AA B⁰AT1 and ATB⁰, transporters.&quot;,
            }
        }

        for amino_acid_name in [&quot;Alanine&quot;, &quot;Arginine&quot;, &quot;L_Asparagine&quot;, &#39;Aspartate&#39;, &#39;Cysteine&#39;, &#39;Glutamate&#39;, &#39;L_Glutamine&#39;, &#39;Glycine&#39;, &#39;Histidine&#39;, &#39;Isoleucine&#39;, &#39;Leucine&#39;, &#39;Lysine&#39;, &#39;Methionine&#39;, &#39;Phenylalanine&#39;, &#39;Proline&#39;, &#39;Serine&#39;, &#39;Threonine&#39;, &#39;Tryptophan&#39;, &#39;Tyrosine&#39;, &#39;Valine&#39;]:
            amino_acid_dict[amino_acid_name] = {
                &quot;text&quot;: text_dict.get(amino_acid_name, {}).get(&quot;text&quot;, &quot;&quot;),
                &quot;image&quot;: amino_acid_name.lower() + &quot;.svg&quot;,
                &quot;use&quot;: amino_acid_name.lower() + &quot;_use&quot;,
                &quot;bacterium&quot;: generate_bacterium_list(amino_acid_name)[:7]
            }

        amino_acids = []

        for amino_acid in amino_acid_dict:
            name = amino_acid
            use = amino_acid_dict[amino_acid][&#39;use&#39;]
            text = amino_acid_dict[amino_acid][&#39;text&#39;]
            image = amino_acid_dict[amino_acid][&#39;image&#39;]
            bacterium = amino_acid_dict[amino_acid][&#39;bacterium&#39;]
            
            amino_acids.append((name, use, text, image, bacterium))

            
        amino_acid_counts = {}
        for amino_acid, field_name, bacterium, text, image in amino_acids:
            consumers = common.filter(**{field_name + &#39;__contains&#39;: &#39;+&#39;})
            common_consumers = consumers.filter(gm_compilations__isnull=False).annotate(compilation_count=Count(&#39;gm_compilations&#39;, distinct=True))
            common_consumers_count = common_consumers.count()
            amino_acid_count = consumers.count()
            total_count = Bacteria.objects.filter(**{field_name + &#39;__contains&#39;: &#39;+&#39;}).count()

            amino_acid_counts[amino_acid] = {
                &#39;common_consumers_count&#39;: common_consumers_count,
                &#39;count&#39;: amino_acid_count,
                &#39;total_count&#39;: total_count,
                &#39;text&#39;: text,
                &#39;image&#39;: image,
                &#39;bacterium&#39;: bacterium,
            }

        context = {
            &#39;bacteria&#39;: bacteria,
            &#39;important_coloniser&#39;: common_count,
            &#39;amino_acid_counts&#39;: amino_acid_counts,
            &#39;amino_acids&#39;: amino_acids,
            &#39;common&#39;: common,
            
        }

        return render(request, self.template_name, context)

And here is the template code:

    &lt;table class=&quot;table table-sortable table-bordered table-hover&quot;&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;Amino acid&lt;/th&gt;
                &lt;th&gt;Common&lt;/th&gt;
                &lt;th&gt;Example species&lt;/th&gt;
                &lt;th&gt;All consumers&lt;/th&gt;
                &lt;th&gt;Notes&lt;/th&gt;
                &lt;th&gt;Image&lt;/th&gt;
            &lt;/tr&gt;

        &lt;/thead&gt;
        &lt;tbody&gt;
            {% for amino_acid, field_name, text, image, bacterium in amino_acids %}
            &lt;tr&gt;
                &lt;td&gt;
                    {{ amino_acid }}
                &lt;/td&gt;
                &lt;td&gt;
                    {{ amino_acid_counts|dict_lookup:amino_acid|default_if_none:&#39;&#39;|dict_lookup:&#39;count&#39; }}
                &lt;/td&gt;
                &lt;td&gt;
                    {% for bact in bacterium %}
                    &lt;i&gt;&lt;a href=&quot;/bacteria/{{ bact|slugify|lower }}&quot; target=&quot;_blank&quot;&gt;{{ bact }};&lt;/a&gt;&lt;/i&gt;
                    {% endfor %}
                &lt;/td&gt;
                &lt;td&gt;
                    {{ amino_acid_counts|dict_lookup:amino_acid|default_if_none:&#39;&#39;|dict_lookup:&#39;total_count&#39; }}
                &lt;/td&gt;
                &lt;td&gt;
                    {{ text }}
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;img src=&quot;/media/image/naturalaa-info/{{ image }}&quot; style=&quot;width: 200px; height: 150px;&quot;&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            {% endfor %}

        &lt;/tbody&gt;

</details>



huangapple
  • 本文由 发表于 2023年5月29日 13:10:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定