2016-10-25 1 views
0

저는 장고를 처음 접했고 장고를 사용하여 블로그를 만드는 데 도움이되는 자습서를 따라했습니다. 그 프로젝트는 괜찮 았어. 그러나 내가 배운 것을 사용하고 싶을 때 튜토리얼을 만들고 나만의 간단한 웹 사이트를 만들었습니다. 나는 오랫동안이 문제에 빠져 있었고, 정말로 나를 괴롭 히고 있습니다. 그래서 새로운 눈 쌍이 문제를 해독하는 데 도움이되기를 바랍니다.

내가 점점 오전 오류는 이것이다 :

지금은 내 원래 프로젝트에서 내 주장의 CD ''를하기 전에 "U" '적이 알고 난 아무 생각이 없다
Reverse for 'course_detail' with arguments '()' and keyword arguments '{u'cd': ''}' not found. 1 pattern(s) tried: ['course/(?P<cd>\\d+)/$'] 

나는이 문제의 원인을 찾기 위해 끊임없이 보았고 나는 그것을 발견 할 수 없다. 여기

이 문제와 관련된 코드의 나머지 부분입니다 :

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 

나는이 작업을 진행하게 생각할 수있는 모든 것을 시도로 희망 누군가가 도움이 될 수 있습니다. 더 이상의 코드가 필요하면 알려주십시오.

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 

내가 대답을 시도하고 나는이 오류를 얻을 코스 이름을 클릭 이니까하지만 지금은 웹 사이트 로딩을 얻고, 일 : 추적 로그는 빈 캡처 그룹와 course_detail URL을 찾을 수 없음을 말하고있다

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

. (N.B. u는 유니 코드를 참조하여 문제와는 아무 상관이 없습니다.)

귀하의 course_detail URL이 cd에 대해 하나 개 이상의 숫자를 예상 :

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

을하지만이 전달됩니다 ... 아무것도!

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

왜? 사용자의보기에서 {% url 'course_detail' cd=course.cd %}, course.cd은 비어 있습니다.

나는 당신의 Course 모델을 볼 필요가있을 것이다, 그러나 나의 의심은 대신 표준 idcd를 사용하여 간단한 있다는 것입니다. 문제를 해결해야하는 course.id으로 전환 해보세요.

UPDATE :

두 번째 오류가 views.py에서 동일한 문제로 인해 발생 : 여기

get_object_or_404(Course, cd=cd) 

cd

가 정의되지 않는다 : 당신은 id로 변경해야합니다.

+0

빠른 답장을 보내 주셔서 감사 드리며 도와 주신 것에 대해 감사드립니다. 나는 당신이 제안한 것을 시도했으나 효과가 있었지만 지금은 다른 오류를 낳았습니다. 나는 원래의 게시물을 새로운 오류로 업데이트했습니다. – Huzzah

+0

@HassnIrshaad updated – brianpck

+0

get_object_or_404 (Course, cd = cd) 대신 get_object_or_404 (Course, cd = course.id)로 변경해야합니까? – Huzzah

관련 문제