Liste les catégories de contenus.
This commit is contained in:
parent
b981d8c656
commit
9c82cb8ee7
7 changed files with 68 additions and 13 deletions
|
@ -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
|
||||
import django.db.models.deletion
|
||||
|
@ -24,9 +24,10 @@ class Migration(migrations.Migration):
|
|||
name='Content',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('Nom du contenu', models.CharField(max_length=255)),
|
||||
('URL du contenu', models.URLField(editable=False)),
|
||||
('group_owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='auth.Group')),
|
||||
('name', models.CharField(max_length=255, verbose_name='Nom du contenu')),
|
||||
('content_url', models.URLField(null=True, verbose_name='URL du contenu')),
|
||||
('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')),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
|
@ -14,14 +14,19 @@ class Content(models.Model):
|
|||
"""Un contenu du site (vidéo)."""
|
||||
name = models.CharField(
|
||||
max_length=255,
|
||||
name="Nom du contenu"
|
||||
verbose_name="Nom du contenu"
|
||||
)
|
||||
group_owner = models.ForeignKey(
|
||||
Group,
|
||||
on_delete=models.CASCADE,
|
||||
editable=False,
|
||||
)
|
||||
content_url = models.URLField(
|
||||
name='URL du contenu',
|
||||
editable=False,
|
||||
verbose_name='URL du contenu',
|
||||
null=True,
|
||||
)
|
||||
category = models.ForeignKey(
|
||||
Category,
|
||||
on_delete=models.SET_NULL,
|
||||
verbose_name="Catégorie",
|
||||
null=True
|
||||
)
|
||||
|
|
17
content/templates/content/content_list.html
Normal file
17
content/templates/content/content_list.html
Normal 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
11
content/urls.py
Normal 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'
|
||||
),
|
||||
]
|
|
@ -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
|
||||
|
|
|
@ -14,11 +14,12 @@ Including another URLconf
|
|||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
from django.urls import include, path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('', views.home),
|
||||
path('content/', include('content.urls')),
|
||||
]
|
||||
|
|
|
@ -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
|
||||
import django.core.validators
|
||||
|
@ -12,8 +12,8 @@ class Migration(migrations.Migration):
|
|||
|
||||
dependencies = [
|
||||
('auth', '0009_alter_user_last_name_max_length'),
|
||||
('content', '0002_content_category'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('content', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
|
|
Loading…
Reference in a new issue