0

我认为与简单的 FK 查找相比,这可能是一个稍微复杂的问题。我正在尝试在我的模板中输出一个列表,但在显示它时遇到了一些困难。我认为这与我的模板中的 for 循环有关。任何帮助,将不胜感激!

模型.py

class Option(models.Model):
    relevantdisease = models.ForeignKey(Disease, verbose_name="disease")
    option = models.CharField(max_length=300, verbose_name="treatment Options")

class State(models.Model):
   state = models.CharField(max_length=300, verbose_name='state')
   relevantdisease = models.ForeignKey(Disease, verbose_name="disease")
   relevantoption = models.ManyToManyField(Option, through='StateOption')

class StateOption(models.Model):
   parttstate = models.ForeignKey(State)
   partoption = models.ForeignKey(Option)
   relevantoutcome = models.ManyToManyField(Outcome, through='StateOptionOutcome')

视图.py

def diseasestateoptionlist(request, disease_id, state_id):

    state = get_object_or_404(State, pk=state_id)
    disease = get_object_or_404(Disease, pk=disease_id)

    stateoption = get_list_or_404(StateOption, parttstate=state_id) #probem may be here or in template.html

    diseasestateoptionlist = get_list_or_404(StateOption, parttstate=state_id)

    context = {'state': state, 'disease':disease, 'diseasestateoptionlist': diseasestateoptionlist, 'stateoption':stateoption}
    return render(request, "diseasestateoptionlist.html", context)

模板.html

{{ disease }}
{{ state }}
{% if diseasestateoptionlist %}
 <ul>
{% for option in diseasestateoptionlist %}
   <li><a href="/drui_index/{{disease.id}}/state/option/{{state.id}}/list">{{stateoption.partoption}}</a></li> 
 #problem may be in the stateoption.partoption call
 {% endfor %}

更新:使用 for 循环正确呈现显示,但未显示 {{stateoption.partoption}} 中的选项。所以 {{disease}}、{{state} 渲染得很好。{diseasestateoptionlist} 显示在表中找到的值的数量。但是没有显示任何值(只是来自

  • 陈述)。

  • 4

    1 回答 1

    1

    {% for diseasestateoptionlist中的选项%}

    ...{{ stateoption .partoption}} ...

    这两个必须相同。:)

    问候

    于 2013-08-25T02:18:04.947 回答