Skip to content

Commit

Permalink
ajout des views basees sur des classes
Browse files Browse the repository at this point in the history
  • Loading branch information
seriadiallo committed Sep 3, 2024
1 parent ea90a78 commit 42aeeb4
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 6 deletions.
9 changes: 8 additions & 1 deletion articles/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms

from .models import Article
from .models import Article, Comment


# class ArticleForm(forms.Form):
Expand All @@ -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', )
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
14 changes: 13 additions & 1 deletion articles/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils import timezone

# titre, contenu, date publication, resume

Expand All @@ -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}"
47 changes: 47 additions & 0 deletions articles/templates/articles/article_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% extends "articles/layout.html" %}
{% block content %}
<h1>{{ article.title }}</h1>
<div class="row">
<div class="col-10 offset-1">
<img style="width: 80%; height: 200px;" src="{% if article.cover %} {{ article.cover.url }} {% endif %}" class="card-img-top" alt="{{ article.title }}">
</div>
</div>
<hr>
<div class="row">
<div class="col-10 offset-1">
{{ article.content }}
</div>
</div>
<hr>
{% for comment in article.comment_set.all %}
<div class="row">
<div class="col-3">
<div class="card text-bg-light mb-3" style="max-width: 18rem;">
<div class="card-header">Header</div>
<div class="card-body">
<h5 class="card-title">{{ comment.created_at }}</h5>
<p class="card-text">{{ comment.content }}</p>
</div>
</div>
</div>
</div>

{% endfor %}
<form action="{% url 'add-comment' article.id %}" method="post">
{% csrf_token %}
<div class="row">
<div class="col-10 offset-1">
<table>
{{ form }}
</table>
</div>
<p>
<button class="btn btn-primary">Enregistrer</button>
</p>
</div>

</form>



{% endblock content %}
24 changes: 24 additions & 0 deletions articles/templates/articles/article_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "articles/layout.html" %}
{% block content %}
<h1>Liste des articles</h1>
<a href="{% url "form-articles" %}">Ajouter</a>
<div class="row">

{% for article in articles %}
<div class="col-md-3">
<div class="card" style="width: 18rem;">
<img src="{% if article.cover %} {{ article.cover.url }} {% endif %}" class="card-img-top" alt="{{ article.title }}">
<div class="card-body">
<h5 class="card-title">{{ article.title }}</h5>
<p class="card-text">{{ article.sumary }}</p>
<a href="{% url 'detail' article.id %}" class="btn btn-primary">Details</a>
</div>
</div>
</div>
{% endfor %}

</div>


{% endblock content %}

9 changes: 7 additions & 2 deletions articles/urls.py
Original file line number Diff line number Diff line change
@@ -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/<int:id>/', get_and_update, name='edit'),
path('detail/<int:pk>/', ArticleDetailView.as_view(), name='detail'),
path('add-comment/<int:id>/', add_comment, name='add-comment'),
]
54 changes: 54 additions & 0 deletions articles/view_class.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 23 additions & 2 deletions articles/views.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 42aeeb4

Please sign in to comment.