微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Django模板中将其模型链接到其外键和manytomany字段?

如何解决如何在Django模板中将其模型链接到其外键和manytomany字段?

我是django的新手,有一个相当基本的问题,但是我无法在线找到答案,因此,如果以前已经回答过这个问题,我们深表歉意...

我有3种型号:

class Type(models.Model):
    name = models.CharField(max_length=100)

class Exercise(models.Model):
    name = models.CharField(max_length=100)
    type = models.ForeignKey(Type,on_delete=models.CASCADE,default=1)

class Area(models.Model):
    name = models.CharField(max_length=100)
    notes = models.TextField(blank=True,null=True)
    exercise = models.ManyToManyField(Exercise)

和3个视图:

class AreaView(ListView):
    model = Area
    template_name = 'workouts/areas.html'
    context_object_name = 'areas'

    def get_context_data(self,**kwargs):
        context = super(AreaView,self).get_context_data(**kwargs)
        return context

class ExerciseView(ListView):
    model = Area
    template_name = 'workouts/exercises.html'
    context_object_name = 'exercises'

    def get_context_data(self,**kwargs):
        context = super(ExerciseView,self).get_context_data(**kwargs)
        return context

class TypeView(ListView):
    model = Exercise
    template_name = 'workouts/types.html'
    context_object_name = 'types'

    def get_context_data(self,**kwargs):
        context = super(TypeView,self).get_context_data(**kwargs)
        return context

我已经将每个视图注册到了这样的网址:

urlpatterns = [
    path('',AreaView.as_view(),name='areas'),path('exercises/<int:id>',ExerciseView.as_view(),name='exercises'),path('types/<int:id>',TypeView.as_view(),name='types'),]

并创建了3个模板area.html:

{% for area in areas %}
    <ul>
        <li><a href="{% url 'exercises' id=area.id %}">{{ area.name }}</a></li>
    </ul>
{% endfor %}

exercises.html:

{% for area in areas %}
    <ul>
        <li><a href="{% url 'types' id=exercise.id %}">{% for exercise in area.exercise.all %}{{ exercise }}{% endfor %}</a></li>
    </ul>
{% endfor %}

and types.html:

{% for exercise in exercises %}
    <ul>
        <li>{% for type in exercise.type.all %}{{ type}}{% endfor %}</li>
    </ul>
{% endfor %}

我向管理员的每个区域添加了特定的练习,并且为每个练习添加了特定的类型。我想做的是显示areas.html中所有区域的完整列表,然后链接exercises.html中其相应练习的列表,然后依次链接显示每个练习的页面类型。希望这是有道理的。。。我尝试过为上下文和循环等使用不同的命名约定,但是到目前为止,没有任何事情可以解决我想要的方式。

我是否需要在视图中定义一个查询集?或者我做错了什么?

解决方法

为此,您可以使用DetailView。

class AreaDetailView(DetailView):
    model = Area
    context_object_name = 'area'
    template_name = 'exercises.html'

然后将此详细信息视图映射到url。

path('area/<int:pk>/detail/',AreaDetailView.as_view(),name='area_detail'),

现在您可以在模板中链接此网址。

{% for area in areas %}
    <ul>
        <li><a href="{% url 'area_detail' area.pk %}">{{ area.name }}</a></li>
    </ul>
{% endfor %}

现在在exercise.html中,您可以像这样获得与该区域相关的练习。

{% for exercise in area.exercise_set.all %}
    <ul>
        <li><a href="{% url 'exercise_detail' id=exercise.id %}">{{ exercise.name }}</a></li>
    </ul>
{% endfor %}

类似地,您可以对ExerciseDetailView使用类似的方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。