Liste les catégories de contenus.

This commit is contained in:
Klafyvel 2018-01-24 11:33:05 +01:00
parent b981d8c656
commit 9c82cb8ee7
7 changed files with 68 additions and 13 deletions

View file

@ -1,4 +1,4 @@
# Generated by Django 2.0.1 on 2018-01-14 18:04 # Generated by Django 2.0.1 on 2018-01-24 10:29
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
@ -24,9 +24,10 @@ class Migration(migrations.Migration):
name='Content', name='Content',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('Nom du contenu', models.CharField(max_length=255)), ('name', models.CharField(max_length=255, verbose_name='Nom du contenu')),
('URL du contenu', models.URLField(editable=False)), ('content_url', models.URLField(null=True, verbose_name='URL du contenu')),
('group_owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='auth.Group')), ('category', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='content.Category', verbose_name='Catégorie')),
('group_owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='auth.Group')),
], ],
), ),
] ]

View file

@ -14,14 +14,19 @@ class Content(models.Model):
"""Un contenu du site (vidéo).""" """Un contenu du site (vidéo)."""
name = models.CharField( name = models.CharField(
max_length=255, max_length=255,
name="Nom du contenu" verbose_name="Nom du contenu"
) )
group_owner = models.ForeignKey( group_owner = models.ForeignKey(
Group, Group,
on_delete=models.CASCADE, on_delete=models.CASCADE,
editable=False,
) )
content_url = models.URLField( content_url = models.URLField(
name='URL du contenu', verbose_name='URL du contenu',
editable=False, null=True,
)
category = models.ForeignKey(
Category,
on_delete=models.SET_NULL,
verbose_name="Catégorie",
null=True
) )

View file

@ -0,0 +1,17 @@
{% extends "base.html" %}
{% block content %}
{% if category %}
<h1>{{category.name}}</h1>
{% else %}
<h1>Liste des contenus</h1>
{% endif %}
{% for content in contents %}
<div>
<h2>{{content.name}}</h2>
<h3>Contenu proposé par {{content.group_owner.name}}</h3>
<a href="{{content.content_url}}">C'est ici que ça se passe</a>
</div>
{% endfor %}
{% endblock %}

11
content/urls.py Normal file
View file

@ -0,0 +1,11 @@
from django.urls import path
from .views import ContentCategoryList
urlpatterns = [
path(
'category/<int:category_id>/',
ContentCategoryList.as_view(),
name='category-list'
),
]

View file

@ -1,3 +1,23 @@
from django.shortcuts import render from django.views.generic import ListView
from django.shortcuts import get_object_or_404
# Create your views here. from .models import Content, Category
class ContentCategoryList(ListView):
"""Affiche les contenus d'une catégorie."""
model = Content
context_object_name = "contents"
template_name = "content/content_list.html"
def get_queryset(self):
category_id = self.kwargs['category_id']
category = get_object_or_404(Category, id=category_id)
return Content.objects.filter(category=category)
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
category_id = self.kwargs['category_id']
category = get_object_or_404(Category, id=category_id)
context['category'] = category
return context

View file

@ -14,11 +14,12 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import include, path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', views.home), path('', views.home),
path('content/', include('content.urls')),
] ]

View file

@ -1,4 +1,4 @@
# Generated by Django 2.0.1 on 2018-01-24 09:37 # Generated by Django 2.0.1 on 2018-01-24 10:29
from django.conf import settings from django.conf import settings
import django.core.validators import django.core.validators
@ -12,8 +12,8 @@ class Migration(migrations.Migration):
dependencies = [ dependencies = [
('auth', '0009_alter_user_last_name_max_length'), ('auth', '0009_alter_user_last_name_max_length'),
('content', '0002_content_category'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL), migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('content', '0001_initial'),
] ]
operations = [ operations = [