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 trialDongyun Cho
2,256 Pointshelp me with my code!
I got stucked while practicing this codes using another variable name!
family app/urls.py
from django.conf.urls import url
from . import views
urlpatterns= [
url(r'^$', views.family, name='family'),
url(r'^parent/', views.parent_list, name='parent_list'),
url(r'^parent(?P<parent_pk>[0-9]+)/(?P<child_pk>[0-9]+)/$}', views.child_detail, name='child_detail'),
url(r'^parent(?P<pk>[0-9]+)/$', views.parent_detail, name='parent_detail'),
]
family app/views.py
from django.shortcuts import render, get_object_or_404
from .models import Parent, Child
# Create your views here.
def family(request):
return render(request, 'family/family.html')
def parent_list(request):
parents = Parent.objects.all()
return render(request, 'family/parent_list.html', {'parents': parents})
def parent_detail(request, pk):
parent = get_object_or_404(Parent, pk=pk)
return render(request, 'family/parent_detail.html', {'parent': parent})
def child_detail(request, parent_pk, child_pk):
child = get_object_or_404(Child, parent_id=parent_pk, pk=child_pk)
return render(request, 'family/child_detail.html', {'child': child})
family/templates/family/parent_detail.html
{% extends 'home.html' %}
{% block content %}
Parent {{ parent }}'s detail<br><br>
{% for child in parent.child_set.all %}
<a href="{% url 'family:child_detail' parent_pk=parent.id child_pk=parent.child.pk %}">{{ child }}</a><br>
{% endfor %}
{% endblock %}
and NoReverseMatch Exception comes out, and I dont know what is wrong. It's the same code as teacher's code though! here's errer message.
NoReverseMatch at /family/parent1/ Reverse for 'child_detail' with arguments '()' and keyword arguments '{'parent_pk': 1, 'child_pk': ''}' not found. 1 pattern(s) tried: ['family/parent(?P<parent_pk>[0-9]+)/(?P<child_pk>[0-9]+)/$}']
it seems this code is wrong, but can anyone tell me why?
<a href="{% url 'family:child_detail' parent_pk=parent.id child_pk=parent.child.pk %}">{{ child }}</a><br>
1 Answer
Haydar Al-Rikabi
5,971 PointsIn your url tag within family/templates/family/parent_detail.html, replace:
child_pk=parent.child.pk
with:
child_pk=child.pk