我正在使用Django 1.8.3和Python 3.4,在点击我网站上的链接时,我面临以下问题。
这是错误页面:
http://nest.azurewebsites.net/elasticsearch-net/handling-responses.html
项目目录:
* cacademy:
* __pycache__
* __init__
* settings
* urls
* wsgi
* Classes:
* __pycache__
* migrations
* static
* __init__
* admin
* models
* tests
* urls
* views
* templates:
* cacademy:
* base.html
* classes_list.html
* courses_detail.html
* db.sqlite3
* manage.py
我的模特:
from django.db import models
from django.utils import timezone
class Course(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
teachername = models.TextField(max_length=200)
teacherinfo = models.TextField()
started_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
我的观点:
from django.shortcuts import render
from django.utils import timezone
from .models import Course
from django.shortcuts import render, get_object_or_404
def classes_list(request):
courses = Course.objects.filter(started_date__lte=timezone.now()).order_by('started_date')
return render(request, 'cacademy/classes_list.html', {'courses': courses})
def course_detail(request, pk):
course = get_object_or_404(Course, pk=pk)
return render(request, 'cacademy/courses_detail.html', {'course': course})
我的网址:
from django.conf.urls import include, url
from Classes import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.classes_list, name='classes_list'),
url(r'^(?P<pk>[0-9]+)/Course/$', views.course_detail, name='Course_detail'),
]
classes_list.html:
{% extends "cacademy/base.html" %}
{% load staticfiles %}
{% block content %}
<div>
<h2> Remember you can only register 3 Courses at once!</h2>
</div>
{% for course in courses %}
<div course="post">
<div course="date">
{{ course.started_date }}
</div>
<h1><a href="{% url 'course_detail' pk=Course.pk %}">{{ course.title }}</a></h1>
<p>Taught by: {{ course.teachername|linebreaks }}</p>
</div>
{% endfor %}
{% endblock content %}
答案 0 :(得分:1)
将模板网址标记更改为
{% url 'course_detail' course.id %}
并在你的urls.py
中url(r'^(?P<pk>[0-9]+)/course/$', views.course_detail, name='course_detail')
答案 1 :(得分:0)
首先,您已在Course_detail
中为课程明细网址指定了反向名称urls.py
,但您在模板中使用反向名称course_detail
。它应该是模板中的Course_detail
。
其次,您将pk
作为Course.pk
传递到模板中。它应该是course.pk
,而不是在迭代course
个对象时使用courses
变量。
而是使用带有小写首字母的反向名称来删除区分大小写的问题。
<强> urls.py 强>
from django.conf.urls import include, url
from Classes import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.classes_list, name='classes_list'),
url(r'^(?P<pk>[0-9]+)/Course/$', views.course_detail, name='course_detail'), # assign reverse name with lowercase initial letter
]
然后在您的模板中,您可以执行此操作:
<强> classes_list.html 强>
{% extends "cacademy/base.html" %}
{% load staticfiles %}
{% block content %}
<div>
<h2> Remember you can only register 3 Courses at once!</h2>
</div>
{% for course in courses %}
<div course="post">
<div course="date">
{{ course.started_date }}
</div>
<h1><a href="{% url 'course_detail' pk=course.pk %}">{{ course.title }}</a></h1>
<p>Taught by: {{ course.teachername|linebreaks }}</p>
</div>
{% endfor %}
{% endblock content %}