我是Django的新手,我遵循了一个帮助用Django创建博客的教程。该项目进展顺利。但是,当我想要使用我从该教程中学到的内容并创建自己的简单网站时,事情就变成了梨形。我现在已经遇到这个问题很久了,这真的让我很烦恼,所以我希望一双新眼睛可以帮助我解决这个问题。
我得到的错误是:
Reverse for 'course_detail' with arguments '()' and keyword arguments '{u'cd': ''}' not found. 1 pattern(s) tried: ['course/(?P<cd>\\d+)/$']
现在我知道在我最初的项目中,我从来没有过&#34; u&#39;&#34;在&#39; cd&#39;之前在我的论点中,我不知道这是从哪里来的,我已经不知疲倦地看到导致这种情况的原因,我无法找到它。
以下是有关此问题的其余代码:
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'),
]
完整跟踪
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
希望有人可以提供帮助,因为我已经尝试了所有可以让它工作的东西。如果需要更多代码,请告诉我。
更新 的 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
我尝试了你的答案并且它有效,我现在正在加载网站,但现在我点击一个课程名称我得到了这个错误:
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
答案 0 :(得分:0)
跟踪日志告诉您,找不到包含空白捕获组course_detail
的{{1}}网址。 (N.B. cd
指的是unicode,与您的问题无关。)
您的u
网址需要course_detail
的一位或多位数字:
cd
但它通过了......没有!
r'^course/(?P<cd>\d+)/$'
为什么呢?因为在您看来,'course_detail' with arguments '()' and keyword arguments '{u'cd': ''}'
,{% url 'course_detail' cd=course.cd %}
是空白的。
我需要查看您的course.cd
模型,但我怀疑您使用Course
代替标准cd
很简单。尝试切换到id
:这应该可以解决问题。
更新:
您的第二个错误是由course.id
中完全相同的问题引起的:
views.py
get_object_or_404(Course, cd=cd)
此处未定义:您应将其更改为cd
。