T
T
tramonatana19692021-07-09 22:09:02
Django
tramonatana1969, 2021-07-09 22:09:02

Why is django-summernote not saving data to the database?

I am new to Django and programming in general. Created a comment system where there is a parent comment and a child comment (an answer to the parent). And I attached a WYSIWYG summernote editor to the form for editing text (as in stackoverflow). When writing the main comment (parent) everything displays. And when sending the child, it simply reloads the page and nothing appears (respectively, it does not save to the database either) I am new to Django and programming in general. Created a comment system where there is a parent comment and a child comment (an answer to the parent). And I attached a WYSIWYG summernote editor to the form for editing text (as in stackoverflow). When writing the main comment (parent) everything displays. And when sending the child, it simply reloads the page and nothing appears (respectively,
models.py

class Comments(models.Model):
    parent = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        blank=True,
        null=True,
        related_name='Replies',
    )
    author = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        verbose_name='Author',
        related_name='Comments'
    )
    article = models.ForeignKey(
        News,
        on_delete=models.CASCADE,
        related_name='Comments',
        verbose_name='Article'
    )
    text = models.TextField(
        verbose_name='Text',
    )
    created_date = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Created date',
    )
    comment_like = models.IntegerField(
        verbose_name='Like',
        default=0,
    )
    comment_dislike = models.IntegerField(
        verbose_name='Dislike',
        default=0,
    )

    class Meta:
        ordering = ['created_date']

    def __str__(self):
        return f'{self.author}, {self.text}'

views.py
def create_comment(request, item):
    comment_form = CommentForm(request.POST)
    if comment_form.is_valid():
        parent_obj = None
        try:
            parent_id = int(request.POST.get('parent_id'))
        except:
            parent_id = None
        if parent_id:
            parent_obj = Comments.objects.get(id=parent_id)
            if parent_obj:
                replay_comment = comment_form.save(commit=False)
                replay_comment.parent = parent_obj
        new_comment = comment_form.save(commit=False)
        new_comment.article = item
        new_comment.author = request.user
        new_comment.save()
        return new_comment

def news_items(request, news_pk):
    items = get_object_or_404(News, pk=news_pk)
    comments = Comments.objects.filter(article=news_pk)
    best_comment = None
    for i in comments:
        if i.comment_like > 0:
            best_comment = comments.order_by('-comment_like').first()
    if request.method == 'POST':
        create_comment(request, items)
        return redirect('news:news_items', news_pk=items.pk)
    else:
        comment_form = CommentForm()
    context = {
        'items': items,
        'categories': category(),
        'comments': comments,
        'comment_form': comment_form,
        'best_comment': best_comment,
    }
    return render(request, 'news/news_items.html', context)

forms.py
from django import forms
from django_summernote.widgets import SummernoteWidget

from news.models import Comments


class CommentForm(forms.ModelForm):
    text = forms.CharField(widget=SummernoteWidget())

    class Meta:
        model = Comments
        fields = (
            'text',
        )

snippet of code from the template:
<!--Для основного-->
<div class="comment-form">
    <div class='form-wrap'>
        <form action="." method="post" style="display: flex; width: 100%">
            {{ comment_form.media }}
            {{ comment_form.text }}
            {% csrf_token %}
            <div style="padding-left: 10px;">
                 <input class="btn btn-primary" type="submit" value="Высказаться"></div>
        </form>
     </div>
</div>

<!--Для дочернего-->

<div class="reply-form">
    <div class='reply-form-wrap'>
        <form action="." method="post" style="display: flex; width: 100%">
            {{ comment_form.media }}
            {{ comment_form.text }}
            {% csrf_token %}
            <input type="hidden" name="parent_id" value="{{ comment.id }}">
            <div style="padding-left: 10px;">
                <input class="btn btn-primary" type="submit" value="Ответить">
            </div>
        </form>
    </div>
</div>

Help, please, to understand. Or at least explain why it doesn't work. Because under the same conditions, only when using the standard Jung form, everything works well.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question