2017-07-16 83 views
0

我正在設計一個視頻渲染兒童網站。過濾Queryset並在Django中設計正確的urls.py和模板

  • 應用程序有父類別爲如:卡通,教育等顯示每個類別有多個帖子如Jungle_book,Duck_tales等的漫畫。
  • 每個有多個情節
  • 我對視圖使用通用視圖(listview,Detailview)。

這裏是我的Models.py

from django.db import models 
from personal.models import Post 


class cartoons_post(models.Model): 
    category= models.ForeignKey(Post, on_delete=models.CASCADE) 
    title = models.CharField(max_length = 140) 
    thumbnail=models.ImageField() 
    date=models.DateField() 


    def __unicode__(self): 
     return self.title 

    def __str__(self): 
     return self.title 

    def get_absolute_url(self): 
     return reverse("posts:detail", kwargs={"id": self.id}) 


class post_episode(models.Model): 
    post_id= models.ForeignKey(cartoons_post, on_delete=models.CASCADE) 
    title = models.CharField(max_length = 140) 
    thumbnail=models.ImageField() 
    date=models.DateField() 
    video=models.FileField() 


    def __unicode__(self): 
     return self.title 

    def __str__(self): 
     return self.title 

    def get_absolute_url(self): 
     return reverse("posts:detail", kwargs={"id": self.id}) 

這裏是我的urls.py

from django.conf.urls import url, include 
from django.views.generic import ListView, DetailView 
from cartoons.models import cartoons_post,post_episode 
from django.contrib.auth.decorators import login_required 

urlpatterns = [ 

       url(r'^$', login_required(ListView.as_view(
           queryset=cartoons_post.objects.all().order_by("-date")[:10], 
           template_name="cartoons/cartoons.html"))), 
       url(r'^(?P<pk>\d+)$', login_required(ListView.as_view(
           queryset=post_episode.objects.filter(post_id=1).order_by("-date"), 
           template_name="cartoons/post.html"))), 
       url(r'^(?P<pk>\d+\d+)$',login_required(DetailView.as_view(
            model = post_episode, 
            template_name="cartoons/post_episode.html"))), 
      ] 

這裏是我的三個模板

cartoons.html

{% extends "personal/header.html" %} 
 
{% load staticfiles %} 
 
{% block content %} 
 
{% for cartoons_post in object_list %} \t 
 
\t <a href="/cartoons/{{cartoons_post.id}}"> <img src= "{{cartoons_post.thumbnail.url}}" width="200" height="220"> </a> 
 
{% endfor %}  
 
{% endblock %}

post.html

{% extends "personal/header.html" %} 
 
{% load staticfiles %} 
 
{% block content %} 
 
{% for post_episode in object_list %} \t 
 
\t <a href="/cartoons/{{post_episode.id}}"> <img src= "{{post_episode.thumbnail.url}}" width="200" height="220"> </a> 
 
{% endfor %} \t 
 
{% endblock %}

post_episode.html

{% extends "personal/header.html" %} 
 
{% load staticfiles %} 
 
{% block content %} 
 
\t <div class="container-fluid_cartoons"> 
 
\t <h3>{{post_episode.title}}</h3> 
 
\t <video width="850" height="500" controls> 
 
    \t <source src="{{post_episdoe.video.url}}" type="video/mp4"> 
 
\t Your browser does not support the video tag. 
 
\t </video> 
 
\t </div> \t 
 
{% endblock %}

問題 - 只有cartoons.html工作正常。

  • 一旦我點擊一個單獨的帖子,相關的post.html將顯示上傳的所有情節,而不是與個人帖子有關的情節。

  • 一旦我點擊一集,什麼都不會發生。

+0

您應該使用@ python_2_unicode_compitable而不是定製既__unicode__和__str__ – williezh

+0

感謝@ williezh –

回答

0

您應該考慮創建views.py文件,而不是在urls.py中使用通用視圖。我認爲問題是與以下行

url(r'^(?P<pk>\d+)$',login_required(ListView.as_view(queryset=post_episode.objects.filter(post_id=1).order_by("-date"),template_name="cartoons/post.html"))), 

在這裏,如果我的猜測是正確的,你試圖傳遞後模型的id從網址查看。但爲了讓ListView接受url kwargs(post_id),你必須在views.py中編寫另一個視圖。也許像下面的東西。

class EpisodeListView(ListView): 
    template_name = 'cartoons/post.html' 
    def get_queryset(self): 
     return post_episode.objects.filter(post_id=self.kwargs['post_id']).order_by("-date") 

然後路由器的URL到視圖:

from <yourapp.views> import EpisodeListView 
from django.contrib.auth.decorators import login_required 
url(r'^episodes/(?P<pk>\d+)$',login_required(EpisodeListView.as_view())