diff --git a/README.md b/README.md index 307ad92..24c5189 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # Blog Simple blog management system. + +## Todo +1. Fix comment form on mobile +2. Display messages nicely diff --git a/articles/static/style.css b/articles/static/style.css index 3b66fbc..82bb89d 100644 --- a/articles/static/style.css +++ b/articles/static/style.css @@ -173,6 +173,20 @@ form button[type=submit] { cursor: pointer; } +form table tr.error, .errorlist { + color: red; +} + +.errorlist { + margin-top: 0; + list-style-type: none; + padding-left: 0; +} + +.errorlist.nonfield { + margin-bottom: 0; +} + .comment { background-color: var(--background2); border-radius: .5ex; diff --git a/articles/templates/articles/article_detail.html b/articles/templates/articles/article_detail.html index 172aba2..1d73932 100644 --- a/articles/templates/articles/article_detail.html +++ b/articles/templates/articles/article_detail.html @@ -31,7 +31,7 @@
diff --git a/articles/views/html.py b/articles/views/html.py index 2016cf5..a1fc3cc 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -1,8 +1,8 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import F -from django.http import HttpResponseRedirect from django.views import generic +from django.views.generic.edit import FormMixin from articles.forms import CommentForm from articles.models import Article @@ -34,8 +34,9 @@ class DraftsListView(generic.ListView, LoginRequiredMixin): return context -class ArticleDetailView(generic.DetailView): +class ArticleDetailView(FormMixin, generic.DetailView): model = Article + form_class = CommentForm context_object_name = "article" queryset = Article.with_pages.all() template_name = "articles/article_detail.html" @@ -47,12 +48,11 @@ class ArticleDetailView(generic.DetailView): return queryset.filter(status=Article.PUBLISHED) def get_context_data(self, **kwargs): - context = super(ArticleDetailView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) article = self.object if hasattr(article, "article"): article = article.article context["comments"] = article.comments.filter(approved=True) - context["comment_form"] = CommentForm() return context def get_object(self, queryset=None): @@ -65,15 +65,23 @@ class ArticleDetailView(generic.DetailView): return obj + def post(self, request, *args, **kwargs): + self.object = self.get_object() + form = self.get_form() + if form.is_valid(): + return self.form_valid(form) + else: + return self.form_invalid(form) -class CreateCommentView(generic.CreateView): - model = Article - form_class = CommentForm + def form_invalid(self, form): + return super().form_invalid(form) def form_valid(self, form): - self.object = self.get_object() - self.comment = form.save(commit=False) - self.comment.article = self.object - self.comment.save() + comment = form.save(commit=False) + comment.article = self.object + comment.save() messages.success(self.request, "Comment successfully saved.") - return HttpResponseRedirect(self.get_success_url()) + return super().form_valid(form) + + def get_success_url(self): + return self.object.get_absolute_url() diff --git a/blog/urls.py b/blog/urls.py index cb56bdd..e5a76e3 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -23,9 +23,9 @@ urlpatterns = [ path("", html.ArticlesListView.as_view(), name="articles-list"), path("drafts/", html.DraftsListView.as_view(), name="drafts-list"), path("feed/", feeds.CompleteFeed(), name="complete-feed"), - path("