Handle errors in form

This commit is contained in:
Gabriel Augendre 2020-08-18 19:44:13 +02:00
parent 63a41918b5
commit 261120275d
5 changed files with 41 additions and 15 deletions

View file

@ -1,3 +1,7 @@
# Blog # Blog
Simple blog management system. Simple blog management system.
## Todo
1. Fix comment form on mobile
2. Display messages nicely

View file

@ -173,6 +173,20 @@ form button[type=submit] {
cursor: pointer; 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 { .comment {
background-color: var(--background2); background-color: var(--background2);
border-radius: .5ex; border-radius: .5ex;

View file

@ -31,7 +31,7 @@
<form action="{% url 'create-comment' slug=article.slug %}" method="post"> <form action="{% url 'create-comment' slug=article.slug %}" method="post">
{% csrf_token %} {% csrf_token %}
<table> <table>
{{ comment_form.as_table }} {{ form.as_table }}
</table> </table>
<button type="submit">Submit</button> <button type="submit">Submit</button>
</form> </form>

View file

@ -1,8 +1,8 @@
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import F from django.db.models import F
from django.http import HttpResponseRedirect
from django.views import generic from django.views import generic
from django.views.generic.edit import FormMixin
from articles.forms import CommentForm from articles.forms import CommentForm
from articles.models import Article from articles.models import Article
@ -34,8 +34,9 @@ class DraftsListView(generic.ListView, LoginRequiredMixin):
return context return context
class ArticleDetailView(generic.DetailView): class ArticleDetailView(FormMixin, generic.DetailView):
model = Article model = Article
form_class = CommentForm
context_object_name = "article" context_object_name = "article"
queryset = Article.with_pages.all() queryset = Article.with_pages.all()
template_name = "articles/article_detail.html" template_name = "articles/article_detail.html"
@ -47,12 +48,11 @@ class ArticleDetailView(generic.DetailView):
return queryset.filter(status=Article.PUBLISHED) return queryset.filter(status=Article.PUBLISHED)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(ArticleDetailView, self).get_context_data(**kwargs) context = super().get_context_data(**kwargs)
article = self.object article = self.object
if hasattr(article, "article"): if hasattr(article, "article"):
article = article.article article = article.article
context["comments"] = article.comments.filter(approved=True) context["comments"] = article.comments.filter(approved=True)
context["comment_form"] = CommentForm()
return context return context
def get_object(self, queryset=None): def get_object(self, queryset=None):
@ -65,15 +65,23 @@ class ArticleDetailView(generic.DetailView):
return obj 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): def form_invalid(self, form):
model = Article return super().form_invalid(form)
form_class = CommentForm
def form_valid(self, form): def form_valid(self, form):
self.object = self.get_object() comment = form.save(commit=False)
self.comment = form.save(commit=False) comment.article = self.object
self.comment.article = self.object comment.save()
self.comment.save()
messages.success(self.request, "Comment successfully saved.") 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()

View file

@ -23,9 +23,9 @@ urlpatterns = [
path("", html.ArticlesListView.as_view(), name="articles-list"), path("", html.ArticlesListView.as_view(), name="articles-list"),
path("drafts/", html.DraftsListView.as_view(), name="drafts-list"), path("drafts/", html.DraftsListView.as_view(), name="drafts-list"),
path("feed/", feeds.CompleteFeed(), name="complete-feed"), path("feed/", feeds.CompleteFeed(), name="complete-feed"),
path("<slug:slug>", html.ArticleDetailView.as_view(), name="article-detail"), path("<slug:slug>", html.ArticleDetailView.as_view(), name="article-detail-old"),
path("<slug:slug>/", html.ArticleDetailView.as_view(), name="article-detail"), path("<slug:slug>/", html.ArticleDetailView.as_view(), name="article-detail"),
path( path(
"<slug:slug>/comment/", html.CreateCommentView.as_view(), name="create-comment" "<slug:slug>/comment/", html.ArticleDetailView.as_view(), name="create-comment"
), ),
] ]