Generation

code to explanationTue, 01 Aug 2023

from django.shortcuts import render, get_object_or_404, redirect from django.views import View from .models import Tutorial, Comment from .forms import CommentForm class TutorialPageView(View): def get(self, request): tutorials = Tutorial.objects.all() return render(request, 'templates/tutorial/tutorial_page.html', {'tutorials': tutorials}) class TutorialDetailView(View): def get(self, request, tutorial_id): tutorial = get_object_or_404(Tutorial, id=tutorial_id) comments = tutorial.comments.order_by('-created_at') form = CommentForm() return render(request, 'templates/tutorial/tutorial_detail.html', {'tutorial': tutorial, 'comments': comments, 'form': form}) def post(self, request, tutorial_id): tutorial = get_object_or_404(Tutorial, id=tutorial_id) comments = tutorial.comments.order_by('-created_at') form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.tutorial = tutorial if not request.user.is_authenticated: comment.created_by = None else: comment.created_by = request.user comment.save() form = CommentForm() # Réinitialise le formulaire return render(request, 'templates/tutorial/tutorial_detail.html', {'tutorial': tutorial, 'comments': comments, 'form': form}) def like_tutorial(request, tutorial_id): tutorial = get_object_or_404(Tutorial, id=tutorial_id) tutorial.likes += 1 # Augmentez le compteur de likes du tutoriel tutorial.save() return redirect('tutorial_detail', tutorial_id=tutorial_id)

## Authors * **Esther Waweru** - *Initial work* - [HexCode](https://github.com/HexCode254) ## License This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details ## Acknowledgments * [The Odin Project](https://www.theodinproject.com/)

Questions about programming?Chat with your personal AI assistant