Hassn Irshaad Hassn Irshaad - 1 month ago 9
HTML Question

Reverse for 'course_detail' with arguments '()' and keyword arguments '{u'cd': ''}' not found. 1 pattern(s) tried: ['courses/(?P<cd>\\d+)/$']

I am fairly new to Django and I followed a tutorial which helped create a blog using Django. That project went fine. But when I wanted to use what I learnt form that tutorial and make my own simple website that's when things went pear shaped. I have been at this problem for a long time now and it is really bugging me, so I hoped a fresh pair of eyes could help me decode the issue.

The error I am getting is this:

Reverse for 'course_detail' with arguments '()' and keyword arguments '{u'cd': ''}' not found. 1 pattern(s) tried: ['course/(?P<cd>\\d+)/$']


Now I know that in my original project I never had a "u'" before the 'cd' in my arguments and I have no idea where this is coming from, I have looked tirelessly for what is causing this and I can't find it.

Here is the rest of the code regarding this issue:

course_list.html

{% load staticfiles %}
<html>
<head>
<title>Courses</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/website.css' %}">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet" type="text/css">
</head>
<body>
<div class="links">
<h1><a href="">Home</a> |
<a href="">Courses</a> |
<a href="">Venues</a> |
<a href="">About Us</a> |
<a href="">Contact Us</a></h1>
</div>

<div class="page-header">
<h1><a href="">Courses</a></h1>
</div>

<div class="content container">
<div class="row">
<div class="col-md-8">
{% for course in courses %}
<div class="course">
<h2><a href="{% url 'course_detail' cd=course.cd %}">{{ course.course_name }}</a></h2>
<p><b>Course code: </b> {{ course.course_code }}</p>
<p><b>Price: </b> {{ course.price }} </p>
<p><b>Course topic: </b> {{ course.topic_details }} </p>
<div class="date">
<p><b>Start Date: </b> {{ course.start_date }}</p>
<p><b>End Date: </b> {{ course.end_date }}</p>
</div>
<p><b>Availability: </b> {{ course.status }}</p>
<p><b>Course venue: </b> {{ course.venue }}</p>
<p><b>Room course will be held in: </b> {{ course.room }}</p>

<button class="button">Book Course</button>

</div>
{% endfor %}
</div>
</div>
</div>
<div class="footer">
<h3>Other Links</h3>
</div>

</body>

</html>


course_detail.html

{% extends 'website/base.html' %}
{% block content %}
<div class="course">
<p><b>Course code: </b> {{ course.course_code }}</p>
<p><b>Price: </b> {{ course.price }} </p>
<p><b>Course topic: </b> {{ course.topic_details }} </p>
<div class="date">
<p><b>Start Date: </b> {{ course.start_date }}</p>
<p><b>End Date: </b> {{ course.end_date }}</p>
</div>
<p><b>Availability: </b> {{ course.status }}</p>
<p><b>Course venue: </b> {{ course.venue }}</p>
<p><b>Room course will be held in: </b> {{ course.room }}</p>

<button class="button">Book Course</button>

</div>

{% endblock %}


views.py

from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from .models import Course

# Create your views here.
def course_list(request):
courses = Course.objects.all()
return render(request, 'website/course_list.html', {'courses': courses})

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


urls.py

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

urlpatterns = [
url(r'^$', views.course_list, name='course_list'),
url(r'^course/(?P<cd>\d+)/$', views.course_detail, name='course_detail'),
]


Full trace

Request Method: GET
Request URL: https://yr4-group-project-mfblack.c9users.io/
Django Version: 1.9
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'course_detail' with arguments '()' and keyword arguments '{u'cd': ''}' not found. 1 pattern(s) tried: ['course/(?P<cd>\\d+)/$']
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 508
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/ubuntu/workspace',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7']
Server time: Tue, 25 Oct 2016 16:08:46 +0000


Hopefully someone can help as I have tried all the things I can think of to get it working. If there is need of any more code then please let me know.

UPDATE:
models.py

class Course(models.Model):
MY_CHOICES = (
('Open', 'Open'),
('Closed', 'Closed'),
('Fully Booked', 'Fully Booked'),
)
course_name = models.CharField(max_length=40)
course_code = models.CharField(max_length=40)
price = models.CharField(max_length=40, default='add price')
topic_details = models.TextField(max_length=200)
start_date = models.DateField('start date')
end_date = models.DateField('end date')
status = models.CharField(max_length=20, choices=MY_CHOICES)
venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
room = models.CharField(max_length=20)
def __str__(self):
return self.course_name


I tried your answer and it worked, I am getting the website loading now but now that I click on the a course name I get this error:

FieldError at /course/1/
Cannot resolve keyword 'cd' into field. Choices are: course_code, course_name, end_date, id, price, room, start_date, status, student, topic_details, venue, venue_id
Request Method: GET
Request URL: https://yr4-group-project-mfblack.c9users.io/course/1/
Django Version: 1.9
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'cd' into field. Choices are: course_code, course_name, end_date, id, price, room, start_date, status, student, topic_details, venue, venue_id
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py in names_to_path, line 1330
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/ubuntu/workspace',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7']
Server time: Tue, 25 Oct 2016 17:14:37 +0000

Answer

The trace log is telling you that it cannot find a course_detail url with a blank captured group cd. (N.B. The u refers to unicode and has nothing to do with your issue.)

Your course_detail url expects one or more digits for cd:

r'^course/(?P<cd>\d+)/$'

But it is passed...nothing!

'course_detail' with arguments '()' and keyword arguments '{u'cd': ''}'

Why? Because in your view, {% url 'course_detail' cd=course.cd %}, course.cd is blank.

I would need to see your Course model, but my suspicion is that you are simple using cd instead of the standard id. Try switching to course.id: that should fix the issue.

UPDATE:

Your second error is caused by the exact same issue in views.py:

get_object_or_404(Course, cd=cd)

cd here is undefined: you should change it to id.