特定のコースに属するコンテンツのリストにアクセスしたい。アクセスするには、ビューと URL をどのように記述すればよいですか。コースリストのビューと URL を書きましたが、各コース内のコンテンツにアクセスできない場合があります
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)),
)
project\course\templates\course\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 %}
プロジェクト\コース\テンプレート\コンテンツ\コンテンツリスト.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 には値があります。誰か助けてくれませんか