我想访问属于特定课程的内容列表。我应该如何写我的观点和网址来访问它。我已经为课程列表编写了视图和网址,但有些无法访问每门课程中的内容
from django.contrib.auth.models import User
from django.db import models
import datetime
from django.utils import timezone
from django.template.defaultfilters import slugify
class PostManager(models.Manager):
def live(self):
return self.model.objects.filter(published=True)
class Course(models.Model):
course_name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, blank=True, default='')
pub_date = models.DateTimeField('date published')
author = models.ForeignKey(User, related_name="courses")
published_course = models.BooleanField(default=True)
objects = PostManager()
class Meta:
ordering = ["-pub_date", "course_name"]
def __unicode__(self):
return self.course_name
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date < now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently ?'
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.course_name)
super(Course, self).save(*args, **kwargs)
@models.permalink
def get_absolute_url(self):
return ("course:detail", (), {"slug": self.slug})
class Content(models.Model):
course = models.ForeignKey(Course)
topic_name = models.CharField(max_length=200)
slug_topic_name = models.SlugField(max_length=200, blank=True, default='')
desc = models.TextField()
published = models.BooleanField(default=True)
objects = PostManager()
def __unicode__(self):
return self.topic_name
def save(self, *args, **kwargs):
if not self.slug_topic_name:
self.slug_topic_name = slugify(self.topic_name)
super(Content, self).save(*args, **kwargs)
@models.permalink
def get_absolute_url(self):
return ("content:detail", (), {"slug_topic_name": self.slug_topic_name})
我的课程/views.py
from django.views.generic import ListView, DetailView
from .models import Course, Content
class CourseListView(ListView):
model = Course
def get_queryset(self):
queryset = super(CourseListView, self).get_queryset()
return queryset.filter(published_course=True)
class CourseContentListView(ListView):
context_object_name = 'content_list'
queryset = Content.objects.filter(course__course_name='HTML5')
template_name = 'content/content_list.html'
我的课程/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r"^$", views.CourseListView.as_view(), name="list" ),
url(r"^(?P<course_name>[\w-]+)/$", views.CourseContentListView.as_view(), name="list"),
)
项目/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from . import views
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'kodeworms.views.home', name='home'),
# url(r'^kodeworms/', include('kodeworms.foo.urls')),
url(r"^$", views.HomepageView.as_view(), name="home"),
url(r"^course/", include("course.urls", namespace="course")),
url(r"^course/content", include("course.urls", namespace="content")),
url(r'^admin/', include(admin.site.urls)),
)
项目\课程\模板\课程\course_list.html
{% extends "_layouts/base.html" %}
{% load url from future %}
{% block page_title %}Course List | {% endblock %}
{% block page_content %}
<h2>Course Grid</h2>
<ul>
{% for course in course_list %}
<li><a href="{% url 'content:list' %}">{{ course.course_name }}</a></li>
{% empty %}
<li>Sorry, no courses yet. Check back soon! </li>
{% endfor %}
<p><a href="{% url 'home' %}">← Home</a></p>
</ul>
{% endblock %}
项目\课程\模板\内容\content_list.html
{% extends "_layouts/base.html" %}
{% block page_title %}{{ course.course_name }} | {% endblock %}
{% block page_content %}
<h2>{{ course.course_name }}</h2>
{{ course.content|linebreaks }}
<ul>
{% for content in object_list %}
<li>{{ content.topic_name }}</li>
{% empty %}
<li>Sorry, no content yet. Check back soon! </li>
{% endfor %}
</ul>
<p><a href="{% url 'course:list' %}">← Course List</a></p>
{% endblock %}
在上面的代码中,我已经硬编码了课程名称的内容。我想为任何任意课程动态获取它。我的 pk=1 也给出错误, pk=8/9/10 有值。有人可以帮忙吗