【发布时间】:2018-09-11 23:44:56
【问题描述】:
大家好,我目前正在处理 django 视图和模板,但遇到了一些问题。 我有一个名为“代谢物”的模型,其中包含:id、名称、隔间、电荷和公式 5 组件。 我有另一个名为“Reactionsmeta”的模型,其中包含:id(reactions)、name、metabolie1、metabolite2、....metabolite6。这个模型的表格没有填写完整,因为有时一个id对应5个代谢物,但有时甚至20个。
我写了一个可以显示所有反应的模板,当我点击反应进入详情页面时,我也想显示这个反应涉及的代谢物。我的views.py和模板如下:
reactions_detail.html
{% extends 'Recon/Base.html' %}
{% load static %}
{% block title %}Reaction Details{% endblock %}
{% block body %}
<h1>{{ reactionsmeta.id }}</h1>
<h2>{{ reactionsmeta.name}}</h2>
<!-- Left Album Info -->
<div class="col-sm-4 col-md-3">
<div class="panel panel-default">
<div class="panel-body">
<a href="{% url 'detail_reaction' reactionsmeta.id %}">
{% if reactionsmeta.id %}
<img src="{% static "Recon/images/Logo-Technische-Universiteit-Eindhoven.jpg" %}" class="img-responsive">
{% else %}
<h3>No image to display</h3>
{% endif %}
</a>
<h1>{{ reactionsmeta.id }} <small>{{ reactionsmeta.name }}</small></h1>
</div>
</div>
</div>
views.py
from django.views import generic
from .models import Reactionsmeta,Metabolites,Reactions
from django.shortcuts import render
class IndexView(generic.ListView):
template_name = 'Recon/index.html'
context_object_name = 'Reactions_object'
def get_queryset(self):
return Reactionsmeta.objects.all()
class DetailsView(generic.DetailView):
model = Reactionsmeta
template_name = 'Recon/reactions_detail.html'
def get_context_data(self, **kwargs):
context = super(DetailsView, self).get_context_data(**kwargs)
context['metabolite'] = Metabolites.objects.all()
context['reactions'] = Reactions.objects.all()
# And so on for more models
return context
如何在reaction_detail.html中编写循环???
编辑:
class Metabolites(models.Model):
id = models.CharField(primary_key=True, max_length=255)
name = models.CharField(max_length=255, blank=True, null=True)
compartment = models.CharField(max_length=255, blank=True, null=True)
charge = models.CharField(max_length=255, blank=True, null=True)
formula = models.CharField(max_length=255, blank=True, null=True)
notes = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'Metabolites'
class Reactionsmeta(models.Model):
id = models.CharField(primary_key=True, max_length=255)
name = models.CharField(max_length=255, blank=True, null=True)
metabolite1 = models.ForeignKey('Metabolites', db_column='metabolite1',
blank=True, null=True, on_delete=models.CASCADE)
stoichiometry1 = models.IntegerField(blank=True, null=True)
metabolite2 = models.CharField(max_length=255, blank=True, null=True)
stoichiometry2 = models.IntegerField(blank=True, null=True)
metabolite3 = models.CharField(max_length=255, blank=True, null=True)
stoichiometry3 = models.CharField(max_length=255, blank=True, null=True)
......
stoichiometry55 = models.CharField(max_length=255, blank=True, null=True)
metabolite56 = models.CharField(max_length=255, blank=True, null=True)
stoichiometry56 = models.CharField(max_length=255, blank=True, null=True)
metabolite57 = models.CharField(max_length=255, blank=True, null=True)
stoichiometry57 = models.CharField(max_length=255, blank=True, null=True)
metabolite58 = models.CharField(max_length=255, blank=True, null=True)
stoichiometry58 = models.CharField(max_length=255, blank=True, null=True)
metabolite59 = models.CharField(max_length=255, blank=True, null=True)
stoichiometry59 = models.CharField(max_length=255, blank=True, null=True)
metabolite60 = models.CharField(max_length=255, blank=True, null=True)
stoichiometry60 = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'ReactionsMeta'
【问题讨论】:
-
我们可以看看您的反应模型、反应元模型和代谢物模型吗?我们可以访问代谢物、反应元和反应之间的关系,但我们需要查看这些字段的名称以及它们是否设置了相关名称。
-
您好,感谢您的关心。我编辑了这个问题并将反应元模型和代谢物模型放在上面。
-
乐于助人。您的
metabolite1字段是外键而其余的是 CharFields 的任何特殊原因?假设 metabolite2-60 实际上是外键是否合理? -
是的,都是外键
-
原因是在后台可以点击代谢物,查看Reactionmeta信息的同时查看该代谢物的信息
标签: python django for-loop django-templates django-views