Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page not found (404) Error on Django:

the output we are looking at is to get title on page http://127.0.0.1:8000/courses/1/1/ and for further pages /2/ or /3/

courses/views.py

from django.shortcuts import get_object_or_404, render

from .models import Course, Step

def course_list(request):
    courses = Course.objects.all()
    return render(request, 'courses/course_list.html',
                  {'courses': courses})

def course_detail(request, pk):
    course = get_object_or_404(Course, pk=pk)
    return render(request, 'courses/course_detail.html',
                  {'course': course})

def step_detail(request, course_pk, step_pk):
    step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/step_detail.html',
                  {'step': step})

our focus will be :

def step_detail(request, course_pk, step_pk):

    step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)

    return render(request, 'courses/step_detail.html', {'step': step})

step_detail.html

{% extends "layout.html" %}

{% block title %} {{ step.title }} - {{ step.course.title }}{% endblock %}
     {% block content %}
     <article>
     <h2> {{step.course.title }} </h2>
         <h3> {{step.title }} </h3>
         {{ step.content|linebreaks }}
     </article>
     {% endblock %}

course/url.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.course_list),
    url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail),
    url(r'(?P<pk>\d+)/$', views.course_detail),
]
like image 573
Ashish Yadav Avatar asked Dec 06 '25 05:12

Ashish Yadav


1 Answers

Actually, you are getting 404 error from here.

 step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)

Here (get_object_or_404) you are saying that if unable to find a Step with given course_id and step_pk then give 404 error and it is doing the same.

You need to make sure that the entries exist. Your codes seem fine.

like image 112
Astik Anand Avatar answered Dec 08 '25 19:12

Astik Anand