[英]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. 我对Django相当陌生,我遵循了一个使用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. 现在我知道在我的原始项目中,我的论点中的“ cd”之前从来没有“ u”,而且我也不知道这是从哪里来的,我一直在不懈地寻找导致它的原因,而我找不到它。
Here is the rest of the code regarding this issue: 以下是有关此问题的其余代码:
course_list.html 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 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 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 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 更新: 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
The trace log is telling you that it cannot find a course_detail
url with a blank captured group cd
. 跟踪日志告诉您无法找到带有空白捕获组cd
的course_detail
URL。 (NB The u
refers to unicode and has nothing to do with your issue.) (注意, u
表示unicode,与您的问题无关。)
Your course_detail
url expects one or more digits for cd
: 您的course_detail
网址应为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. 因为您认为{% url 'course_detail' cd=course.cd %}
, course.cd
为空。
I would need to see your Course
model, but my suspicion is that you are simple using cd
instead of the standard id
. 我需要查看您的Course
模型,但是我怀疑您使用cd
而不是标准id
很简单。 Try switching to course.id
: that should fix the issue. 尝试切换到course.id
:应该可以解决此问题。
UPDATE: 更新:
Your second error is caused by the exact same issue in views.py
: 您的第二个错误是由views.py
完全相同的问题引起的:
get_object_or_404(Course, cd=cd)
cd
here is undefined: you should change it to id
. cd
在这里是未定义的:您应该将其更改为id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.