Commit 3e240412 authored by Давид Ли's avatar Давид Ли

lesson 55

parent 021be428
......@@ -25,6 +25,16 @@ urlpatterns = [
path('articles/add', views.ArticleCreateView.as_view(), name='articles_add'),
path('articles/<int:id>', views.ArticleDetailView.as_view(), name='articles_detail'),
path('articles/<int:id>/update', views.ArticleUpdateView.as_view(), name='articles_update'),
path('articles/<int:id>/delete', views.article_delete_view, name='articles_delete'),
path('articles/<int:id>/delete', views.ArticleDeleteView.as_view(), name='articles_delete'),
path('articles/<int:article_id>/comments/add', views.CommentCreateView.as_view(), name='comments_add'),
path(
'articles/<int:article_id>/comments/<int:comment_id>/update',
views.CommentUpdateView.as_view(),
name='comments_update',
),
path(
'articles/<int:article_id>/comments/<int:comment_id>/delete',
views.CommentDeleteView.as_view(),
name='comments_delete',
),
]
......@@ -37,8 +37,20 @@
{% for comment in comments %}
<div class="card my-4">
<div class="card-header">
{{ comment.author }}
<div class="card-header d-flex justify-content-between align-items-center">
<div class="">{{ comment.author }}</div>
<div class="d-flex">
<a href="{% url 'comments_update' article_id=article.id comment_id=comment.id %}" class="btn btn-warning me-4">
Update
</a>
<form action="{% url 'comments_delete' article_id=article.id comment_id=comment.id %}" method="post" onsubmit="return confirm('Delete?')">
{% csrf_token %}
<button class="btn btn-danger">Delete</button>
</form>
</div>
</div>
<div class="card-body">
......
{% extends 'base.html' %}
{% block content %}
{% include 'partial/form.html' with request_method='post' %}
{% endblock %}
......@@ -9,10 +9,12 @@ from webapp.views.articles import (
ArticleCreateView,
ArticleDetailView,
ArticleUpdateView,
article_delete_view,
ArticleDeleteView,
)
from webapp.views.comments import (
CommentCreateView,
CommentUpdateView,
CommentDeleteView,
)
......
from typing import Any
from django.http import HttpRequest
from django.http.response import HttpResponse as HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse, reverse_lazy
from django.views import generic
from webapp.forms import ArticleForm, CommentForm
......@@ -31,38 +32,107 @@ class ArticleCreateView(generic.CreateView):
return reverse('articles_detail', kwargs={'id': self.object.id})
class ArticleUpdateView(generic.FormView):
template_name = 'articles/article.html'
form_class = ArticleForm
class UpdateView(generic.View):
template_name = None
model = None
form_class = None
context_object_name = None
pk_url_kwarg = None
success_url = None
def dispatch(self, request, *args, **kwargs):
self.article = self.get_object()
self.object = self.get_object()
return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
form = self.form_class(instance=self.object)
return self.render_to_response(form=form)
def post(self, request, *args, **kwargs):
form = self.form_class(instance=self.object, data=request.POST)
if form.is_valid():
return self.form_valid(form)
return self.form_invalid(form)
def get_object(self):
return get_object_or_404(Article, id=self.kwargs.get('id'))
return get_object_or_404(self.model, id=self.kwargs.get(self.pk_url_kwarg))
def get_context_data(self, **kwargs):
return super().get_context_data(
**kwargs,
article=self.article,
btn_text='Update',
)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.article
kwargs[self.context_object_name] = self.object
return kwargs
def get_success_url(self):
return self.success_url
def form_valid(self, form):
self.article = form.save()
return super().form_valid(form)
self.object = form.save()
return redirect(self.get_success_url())
def form_invalid(self, form):
return self.render_to_response(form=form)
def render_to_response(self, **kwargs):
return render(
self.request,
self.template_name,
context=self.get_context_data(**kwargs),
)
class ArticleUpdateView(generic.UpdateView):
template_name = 'articles/article.html'
model = Article
form_class = ArticleForm
context_object_name = 'article'
pk_url_kwarg = 'id'
extra_context = {'btn_text': 'Update'}
def get_success_url(self):
return reverse('articles_detail', kwargs={'id': self.article.id})
return reverse('articles_detail', kwargs={'id': self.object.id})
class DeleteView(generic.View):
template_name = None
model = None
context_object_name = None
pk_url_kwarg = None
success_url = None
def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
self.object = self.get_object()
return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
return self.render_to_response()
def post(self, request, *args, **kwargs):
self.perform_delete()
return redirect(self.get_success_url())
def article_delete_view(request, id):
get_object_or_404(Article, id=id).delete()
def perform_delete(self):
self.object.delete()
return redirect('index')
def get_object(self):
return get_object_or_404(self.model, id=self.kwargs.get(self.pk_url_kwarg))
def get_context_data(self, **kwargs):
kwargs[self.context_object_name] = self.object
return kwargs
def get_success_url(self):
return self.success_url
def render_to_response(self, **kwargs):
return render(
self.request,
self.template_name,
context=self.get_context_data(**kwargs),
)
class ArticleDeleteView(generic.DeleteView):
model = Article
pk_url_kwarg = 'id'
success_url = reverse_lazy('index')
from django.shortcuts import redirect
from django.forms.models import BaseModelForm
from django.http import HttpResponse
from django.views import generic
from django.urls import reverse
from webapp.models import Comment, Article
from webapp.models import Comment
from webapp.forms import CommentForm
from webapp.views.articles import UpdateView, DeleteView
class CommentCreateView(generic.CreateView):
......@@ -21,3 +21,22 @@ class CommentCreateView(generic.CreateView):
comment.save()
return redirect('articles_detail', id=article_id)
class CommentUpdateView(generic.UpdateView):
model = Comment
template_name = 'comments/comment.html'
form_class = CommentForm
context_object_name = 'comment'
pk_url_kwarg = 'comment_id'
def get_success_url(self):
return reverse('articles_detail', kwargs={'id': self.object.article.id})
class CommentDeleteView(generic.DeleteView):
model = Comment
pk_url_kwarg = 'comment_id'
def get_success_url(self):
return reverse('articles_detail', kwargs={'id': self.object.article.id})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment