Ни один Leasson не соответствует заданному запросу. Django

Структура проекта выглядит следующим образом: Категория: Подкатегория: Объект.

реализовал систему next-prev(cat-sub-obj) с трудом. как я могу оптимизировать этот код

когда объект оказывается в подкатегории. Я не могу сразу перейти к следующему объекту в следующей подкатегории.

   def leasson(request, course_slug, section_slug, leasson_slug):
    leasson = get_object_or_404(Leasson, course__slug=course_slug, section__slug=section_slug, slug=leasson_slug)
    section = get_object_or_404(Section, course__slug=course_slug, slug=section_slug)    
    try:
        next_leasson = leasson.get_next_by_date_added(section__slug=section_slug)
    except  Leasson.DoesNotExist:
        next_leasson = None
    try:
        next_section = section.get_next_by_date_added(course__slug=course_slug)
    except  Section.DoesNotExist:
        next_section = None
    try:
        prev_section = section.get_previous_by_date_added(course__slug=course_slug)
    except  Section.DoesNotExist:
        prev_section = None  
    try:
        prev_leasson = leasson.get_previous_by_date_added(section__slug=section_slug)
    except Leasson.DoesNotExist:
        prev_leasson = None
    context = {
        'leasson': leasson,
        'next_leasson':next_leasson,
        'prev_leasson':prev_leasson,
        'next_section':next_section,
        'prev_section':prev_section
    }
    return render (request, 'leasson.html', context)

  path('<slug:course_slug>/<slug:section_slug>/<slug:leasson_slug>/', leasson, name="leasson")

  {% if prev_leasson %}
            <a href="{% url 'leasson' leasson.course.slug leasson.section.slug prev_leasson.slug %}">{{ prev_leasson.title }}</a>         
        {% else %}  
            This is first leasson
        {% endif %}

        {% if next_leasson %}
        <a href="{% url 'leasson' next_leasson.course.slug next_leasson.section.slug next_leasson.slug %}">{{ next_leasson.title }}</a>       
        {% else %}  
            This is last leasson 
        {% endif %}

        {% if prev_section %}
        <a href="{% url 'section' prev_section.course.slug prev_section.slug %}">{{ prev_section }}</a>
        {% else %}
        This is first section
        {% endif %}

        {% if next_section %}
        <a href="{% url 'section' next_section.course.slug next_section.slug %}">{{ next_section }}</a>
        {% else %}
        This is last section
        {% endif %}

с другими ценностями, тогда я все исправлю. это не так важно

Вернуться на верх