我想訪問屬於特定課程的內容列表。我應該如何編寫我的觀點和網址來訪問它。我已經寫了意見和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)),
)
項目\課程\模板\課程\ 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有值。可有人請幫助
向我們展示了故障代碼。 – karthikr
@karthikr對不起後期更新..緩慢的互聯網 – user2539745