Commit aa07ff2d authored by Behr, Svenja's avatar Behr, Svenja
Browse files

- Kommentarformular aufgebaut

- Speichern geht jetzt
- Meldung wird angezeigt
- Kommentiert
parent 95a47617
from django.forms import ModelForm, Textarea, HiddenInput
from blog.models import Comment
class CommentForm(ModelForm):
"""Form to save comments for a blog entry"""
class Meta:
model = Comment
# which fields should be edited?
fields = ['author', 'text', 'related_blog']
# labels for the fields
labels = {'author': 'Name'}
# how should the fields be shown?
widgets = {
'author': Textarea(attrs={'rows': 1}),
'text': Textarea(attrs={'rows': 5}),
'related_blog': HiddenInput()
}
......@@ -10,7 +10,7 @@ function goToDetails(event) {
var targetclassName = card.className.toLowerCase();
if (targetclassName != 'card-deck'){
let id = card.parentNode.children.item(3).innerHTML
let id = card.parentNode.children.item(3).innerHTML;
openDetails(id);
}
}
......
......@@ -16,17 +16,22 @@
.comment-area .author:after {
content: ":"
}
.comment-area blockquote{
background-color: #ffffff;
padding: 0.3em;
}
.comment-area blockquote:before {
content: "„"
}
.comment-area blockquote:after {
content: "“"
}
.comment-area .no-comment{
font-style: italic;
}
.comment-area label:after{
content: ":"
}
\ No newline at end of file
......@@ -19,7 +19,7 @@
</div>
<div class="container">
{% bootstrap_messages %}
<div class="text-right minidate" >{{ blog.date }}</div>
<p class="card-text">{{ blog.description }}</p>
<a href="{% url 'blog:all_blogs' %}" class="btn btn-primary">Back to Blogs</a>
......@@ -31,7 +31,7 @@
{% endif %}
</div>
{% block comments %}
<!-- Comments will co in here -->
<!-- Comments will go in here -->
{% endblock %}
</body>
</html>
\ No newline at end of file
{% extends 'blog/detail.html' %}
{% load bootstrap4 %}
{% block comments %}
<div class="container comment-area">
<h4>Kommentare</h4>
<!-- Show existing comments -->
{% for comment in comments %}
<article>
<div class="row">
......@@ -17,13 +19,13 @@
<div class="col no-comment">Es wurden noch keine Kommentare für diesen Artikel abgegeben.</div>
</div>
{% endif %}
<!-- Add new comments -->
<h5>Schreibe einen Kommentar</h5>
<form>
<label for="comment-input">Text:</label><br>
<textarea id="comment-input" maxlength="1000" required="required"></textarea><br>
<label for="author-input">Name:</label>
<input id="author-input" type="text" maxlength="150" required="required"><br>
<input type="submit">
<form action="{% url 'blog:save_comment' %}" method="POST">
{% csrf_token %}
{% bootstrap_form comment_form %}
<input type="submit" class="btn btn-primary">
</form>
</div>
{% endblock %}
\ No newline at end of file
......@@ -24,4 +24,5 @@ app_name = 'blog'
urlpatterns = [
path('', views.all_blogs, name='all_blogs'),
path('<int:blog_id>/', views.detail, name='detail'),
path('comment/save', views.save_comment, name='save_comment')
]
\ No newline at end of file
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseNotAllowed
from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse_lazy
from .forms import CommentForm
from .models import *
......@@ -14,6 +19,31 @@ def all_blogs(request):
def detail(request, blog_id):
blog = get_object_or_404(Blog, pk=blog_id)
if blog.are_comments_allowed():
# load existing comments
comments = Comment.objects.select_related().filter(related_blog=blog.id)
return render(request, 'blog/detail_with_comments.html', {'blog': blog, 'comments': comments})
# dialog for new comments
form = CommentForm(initial={'related_blog': blog.id})
# render with comments
return render(request, 'blog/detail_with_comments.html',
{'blog': blog, 'comments': comments, 'comment_form': form})
return render(request, 'blog/detail.html', {'blog': blog})
def save_comment(request):
if request.method != 'POST':
# no form submit - return 405
return HttpResponseNotAllowed(['POST'])
form = CommentForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Danke! Dein Kommentar wurde übermittelt.')
else:
messages.error(request, 'Ups! Da ist etwas schief gelaufen. Wir konnten deinen Kommentar nicht speichern!')
print(form.data.get('related_blog'))
blog = get_object_or_404(Blog, pk=form.data.get('related_blog'))
# back to the details page
# todo after merge: Redirect to url from blog article
return redirect(reverse_lazy('blog:detail', kwargs={'blog_id':blog.id}))
No preview for this file type
Supports Markdown
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