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將顯示上傳的所有情節,而不是與個人帖子有關的情節。
一旦我點擊一集,什麼都不會發生。
您應該使用@ python_2_unicode_compitable而不是定製既__unicode__和__str__ – williezh
感謝@ williezh –