Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Final Details url Tag

Error: Page not found (404)

Hi,

I receive this error Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/courses/1/

Using the URLconf defined in Basic_Django.urls, Django tried these URL patterns, in this order: ^courses/ ^$ [name='list'] ^courses/ ^(?P<course_pk>\d+)/(?P<step_pk>\d+)$ [name='step'] ^courses/ ^(?P<pk>\d+)$ [name='course_detail'] ^admin/ ^$ [name='hello_world'] ^static\/(?P<path>.*)$ The current path, courses/1/, didn't match any of these. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Can somebody help me?

Hey Nikolaos,

I'm assuming that the error is coming from naming or syntax somewhere in urls.py or views.py. Without being able to see your code directly, I'll post what worked for me in this exercise, and you can compare from there. I'll continue to look around in the meantime.

First, check that you're making all of the correct imports.

EDIT: My guess is that there's an issue with your primary key(pk) in views.py. If you determine the solution on your own, let me know!

courses/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.course_list, name='list'),
    url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail, name='step'),
    url(r'(?P<pk>\d+)/$', views.course_detail, name='detail'),
]

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})

2 Answers

I would verify your syntax in courses/templates/.

EDIT: Also, if you continue to have issues, if you could post your code wrapped in ```(tick marks to the left of 1 on the keyboard), that would help a lot! I'd probably need to see courses/views.py, and courses/urls.py, at a minimum.

course_detail.html

{% extends "layout.html" %}

{% block title %}{{ course.title }}{% endblock %}

{% block content %}
<article>
    <h2>{{ course.title }}</h2>
    {{ course.description }}

    <section>
        {% for step in course.step_set.all %}
            <h3><a href="{% url 'courses:step' course_pk=step.course.pk step_pk=step.pk %}">{{ step.title }}</a></h3>
            {{ step.description }}
        {% endfor %}
    </section>
</article>
{% endblock %}

course_list.html

{% extends "layout.html" %}

{% block title %}Available Courses{%endblock %}

{% block content %}
<div class="cards">
    {% for course in courses %}
    <div class="cards">
    <header><a href="{% url 'courses:detail' pk=course.pk %}">{{ course.title }}</a></header>
    <div class="card-copy">
        {{ course.description }}
        </div>
    </div>
    {% endfor %}
</div>
{% endblock %}

step_detail.html

{% extends "layout.html" %}

{% block title %}{{ step.title }} - {{ step.course.title }}{% endblock %}

{% block content %}
<article>
    <h2><a href="{% url 'courses:detail' pk=step.course.pk %}">{{ step.course.title }}</a></h2>
    <h3>{{ step.title }}</h3>
    {{ step.content|linebreaks }}
</article>
{% endblock %}

Mr Mauer

Thank you very much for the interest you showed. I had written 2 times the same thing: {{ step.description }}

Thanks

Thanks for your answer.

Now they give me this error:

NoReverseMatch at /courses/1/ Reverse for 'step' with keyword arguments '{'course_pk': '', 'step_pk': 2}' not found. 1 pattern(s) tried: ['courses/(?P<course_pk>\d+)/(?P<step_pk>\d+)/$'] Request Method: GET Request URL: http://127.0.0.1:8000/courses/1/ Django Version: 1.11 Exception Type: NoReverseMatch Exception Value:
Reverse for 'step' with keyword arguments '{'course_pk': '', 'step_pk': 2}' not found. 1 pattern(s) tried: ['courses/(?P<course_pk>\d+)/(?P<step_pk>\d+)/$']