From 42aeeb4d6c2a38340084facf5832ff4de572e7ea Mon Sep 17 00:00:00 2001 From: Sadialiou Diallo Date: Tue, 3 Sep 2024 19:59:09 +0000 Subject: [PATCH] ajout des views basees sur des classes --- articles/forms.py | 9 +++- ..._created_at_article_updated_at_and_more.py | 39 ++++++++++++++ articles/models.py | 14 ++++- .../templates/articles/article_detail.html | 47 ++++++++++++++++ articles/templates/articles/article_list.html | 24 +++++++++ articles/urls.py | 9 +++- articles/view_class.py | 54 +++++++++++++++++++ articles/views.py | 25 ++++++++- 8 files changed, 215 insertions(+), 6 deletions(-) create mode 100644 articles/migrations/0004_article_created_at_article_updated_at_and_more.py create mode 100644 articles/templates/articles/article_detail.html create mode 100644 articles/templates/articles/article_list.html create mode 100644 articles/view_class.py diff --git a/articles/forms.py b/articles/forms.py index 5489b0a..b77d03c 100644 --- a/articles/forms.py +++ b/articles/forms.py @@ -1,6 +1,6 @@ from django import forms -from .models import Article +from .models import Article, Comment # class ArticleForm(forms.Form): @@ -24,3 +24,10 @@ class Meta: 'sumary': forms.TextInput(attrs={'class': 'form-control'}), 'date_pub': forms.TextInput(attrs={'class': 'form-control', 'type': 'date'}), } + + +class CommentForm(forms.ModelForm): + + class Meta: + model = Comment + fields = ('content', ) diff --git a/articles/migrations/0004_article_created_at_article_updated_at_and_more.py b/articles/migrations/0004_article_created_at_article_updated_at_and_more.py new file mode 100644 index 0000000..016641b --- /dev/null +++ b/articles/migrations/0004_article_created_at_article_updated_at_and_more.py @@ -0,0 +1,39 @@ +# Generated by Django 4.2 on 2024-08-30 18:12 + +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('articles', '0003_article_cover'), + ] + + operations = [ + migrations.AddField( + model_name='article', + name='created_at', + field=models.DateTimeField(auto_now_add=True, null=True), + ), + migrations.AddField( + model_name='article', + name='updated_at', + field=models.DateTimeField(auto_now=True, null=True), + ), + migrations.AlterField( + model_name='article', + name='date_pub', + field=models.DateField(default=django.utils.timezone.now, null=True, verbose_name='date de publication'), + ), + migrations.CreateModel( + name='Comment', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.TextField(verbose_name='contenu')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='articles.article')), + ], + ), + ] diff --git a/articles/models.py b/articles/models.py index 779929d..7741985 100644 --- a/articles/models.py +++ b/articles/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.utils import timezone # titre, contenu, date publication, resume @@ -8,8 +9,19 @@ class Article(models.Model): title = models.CharField(max_length=255, verbose_name='titre') sumary = models.CharField(max_length=255, null=True, blank=True, verbose_name='resume') content = models.TextField(verbose_name='contenu') - date_pub = models.DateField(null=True, verbose_name='date de publication') + date_pub = models.DateField(null=True, verbose_name='date de publication', default=timezone.now) cover = models.ImageField(upload_to='articles', max_length=255, null=True, verbose_name="photo de couverture") + created_at = models.DateTimeField(auto_now_add=True, null=True) + updated_at = models.DateTimeField(auto_now=True, null=True) def __str__(self): return self.title + + +class Comment(models.Model): + content = models.TextField(verbose_name='contenu') + created_at = models.DateTimeField(auto_now_add=True) + article = models.ForeignKey(Article, on_delete=models.CASCADE) + + def __str__(self): + return f"{self.created_at}" diff --git a/articles/templates/articles/article_detail.html b/articles/templates/articles/article_detail.html new file mode 100644 index 0000000..534b6cb --- /dev/null +++ b/articles/templates/articles/article_detail.html @@ -0,0 +1,47 @@ +{% extends "articles/layout.html" %} +{% block content %} +

{{ article.title }}

+
+
+ {{ article.title }} +
+
+
+
+
+ {{ article.content }} +
+
+
+ {% for comment in article.comment_set.all %} +
+
+
+
Header
+
+
{{ comment.created_at }}
+

{{ comment.content }}

+
+
+
+
+ + {% endfor %} +
+ {% csrf_token %} +
+
+ + {{ form }} +
+
+

+ +

+
+ +
+ + + +{% endblock content %} \ No newline at end of file diff --git a/articles/templates/articles/article_list.html b/articles/templates/articles/article_list.html new file mode 100644 index 0000000..a4a3c55 --- /dev/null +++ b/articles/templates/articles/article_list.html @@ -0,0 +1,24 @@ +{% extends "articles/layout.html" %} +{% block content %} +

Liste des articles

+ Ajouter +
+ + {% for article in articles %} +
+
+ {{ article.title }} +
+
{{ article.title }}
+

{{ article.sumary }}

+ Details +
+
+
+ {% endfor %} + +
+ + +{% endblock content %} + \ No newline at end of file diff --git a/articles/urls.py b/articles/urls.py index 4aab41f..3cb547b 100644 --- a/articles/urls.py +++ b/articles/urls.py @@ -1,9 +1,14 @@ from django.urls import path -from .views import list_article, formulaire, get_and_update +from .views import list_article, formulaire, get_and_update, add_comment +from .view_class import ArticleListView, ArticleCreateView, ArticleDetailView urlpatterns = [ - path('', list_article, name='list-articles'), + path('', ArticleListView.as_view(), name='list-articles'), + path('class/', ArticleListView.as_view(), name='list-articles-class'), path('ajout/', formulaire, name='form-articles'), + path('ajout-class/', ArticleCreateView.as_view(), name='form-articles-class'), path('edit//', get_and_update, name='edit'), + path('detail//', ArticleDetailView.as_view(), name='detail'), + path('add-comment//', add_comment, name='add-comment'), ] diff --git a/articles/view_class.py b/articles/view_class.py new file mode 100644 index 0000000..fdea112 --- /dev/null +++ b/articles/view_class.py @@ -0,0 +1,54 @@ +from typing import Any +from django.views.generic import ListView, View, CreateView, DetailView +from django.shortcuts import render, redirect +from django.utils import timezone +from django.urls import reverse + +from .models import Article +from .forms import ArticleForm, CommentForm + + +class ArticleListView(ListView): + model = Article + context_object_name = 'articles' + # template_name = 'articles/list-articles.html' + queryset = Article.objects.filter(date_pub__lte=timezone.now()) + + +class ArticleCreateView(CreateView): + model = Article + # fields = ('title', 'sumary', 'cover', 'date_pub', 'content') + form_class = ArticleForm + template_name = 'articles/formulaire.html' + + + + +# class ArticleCreateView(View): +# def get(self, request): +# form = ArticleForm() +# context = { +# 'form': form +# } +# return render(request, 'articles/formulaire.html', context) + +# def post(self, request): +# form = ArticleForm(request.POST, files=request.FILES) +# if form.is_valid(): +# form.save() # enregistrement dans la base +# return redirect(reverse('list-articles')) +# context = { +# 'form': form +# } +# return render(request, 'articles/formulaire.html', context) + + +class ArticleDetailView(DetailView): + model = Article + template_name = 'articles/article_detail.html' + context_object_name = 'article' + + def get_context_data(self, **kwargs: Any) -> dict[str, Any]: + context = super().get_context_data(**kwargs) + context['form'] = CommentForm() + return context diff --git a/articles/views.py b/articles/views.py index 511b283..6b7021e 100644 --- a/articles/views.py +++ b/articles/views.py @@ -1,7 +1,8 @@ +from django.contrib.auth.decorators import login_required from django.shortcuts import render, redirect from django.urls import reverse -from .forms import ArticleForm +from .forms import ArticleForm, CommentForm from .models import Article @@ -12,7 +13,7 @@ def list_article(request): } return render(request, 'articles/list-articles.html', context) - +@login_required def formulaire(request): if request.method == 'POST': form = ArticleForm(request.POST, files=request.FILES) @@ -43,3 +44,23 @@ def get_and_update(request, id): 'form': form } return render(request, 'articles/edit.html', context) + + +def add_comment(request, id): + + if request.method == 'POST': + article = Article.objects.get(id=id) + # initial = {'article': article} + form = CommentForm(request.POST) + if form.is_valid(): + comment = form.save(commit=False) + comment.article = article + comment.save() + else: + context = { + 'article': article, + 'form': form + } + return render(request, 'articles/article_detail.html', context) + return redirect('detail', article.id) +