-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ajout des views basees sur des classes
- Loading branch information
1 parent
ea90a78
commit 42aeeb4
Showing
8 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
articles/migrations/0004_article_created_at_article_updated_at_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters