如何在Django的导航标签中获取相关对象

时间:2019-01-06 23:17:43

标签: django templates navigation

我有三个相关的模型,我想要在导航下的选项卡中获取相关的对象

视图

def quiz_department(request, dept_id):
    department = get_object_or_404(Department, id=dept_id)
    subject = department.subject_set.all()
    return render(request,\
        "multiquiz/department.html",\
        {"department":department, "subject":subject})

def subjects(request, subject_id):
    subject = Subject.objects.get(id=subject_id)
    return render(request, 'multiquiz/subjects.html', {"subject":subject})

department.html

{% extends "base.html" %}

{% block content %}
    <h1>{{department.department}}</h1>
    <ul class="nav nav-tabs">
    {% for sub in subject %}
        <li class="nav-item">
            <a class="nav-link" href="{% url "multichoice:subjects" sub.id %}">{{sub.subjects}}</a>
        </li>
    {% endfor %}
    </ul>  
{% endblock %}

subject.html

{% extends "base.html" %}

{% block content %}
<h1>{{subject.department.department}}</h1>
    <ul class="nav nav-tabs">
        <a href=""></a>
    </ul>
    <div class="container">
    <div class="col-lg-6">  
    <div class="jumbotron">
        {% for question in subject.question_set.all %}
            {{question.text}}
        {% endfor %}
    </div>
</div>
</div>
{% endblock %}

现在我想知道我尝试过无法解决这个问题。 如何获得类似此图片的内容。Subjecs

任何建议,我都会非常感谢指针。 ps:搞错图像错字;)

0 个答案:

没有答案