Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form add tree comments with mptt
#1
I'm confused..
Implemented adding comments using mptt, but for now it works only from admin panel. I can not understand what and where to screw in order to make the addition from the site work. That is what has implemented so far.

If I understand correctly, the logic should be as follows:
1. There is a url that is sent to a particular function in views.py.
2. The view pulls out the form from forms.py and passes it to the template.
3. The user fills out this form and through a POST request sends the data back to this function in views.py.
4. Vha processes the data and sends it back to the template.

As I understand it, at the third stage, the view should receive a parent and add a comment to the list of already existing comments.
It seems that the logic is clear, but with the implementation confused. On the Internet failed to find solutions

class Feedback(MPTTModel):
    author = models.ForeignKey(User, verbose_name='Автор')
    content = models.TextField(blank=True, null=True, verbose_name='Комментарий')
    parent = TreeForeignKey('self', blank=True, null=True, related_name='child')
    date = models.DateTimeField(default=datetime.now, blank=True)
class FeedbackForm(forms.ModelForm):
  class Meta():
    model = Feedback
    fields = ('content',)
def comments_view(request):
    comment_form = FeedbackForm(request.POST)
 
    if comment_form.is_valid():
        content = comment_form.cleaned_data['content']
 
        comment = Feedback()
        comment.content = content
 
        comment.save()
 
    return render(request, 'feedback/comment.html', {'comment_form':comment_form, 'comments': Feedback.objects.all()})
It is necessary that clicking on the "Comment" link triggers the "Add Feedback" button (and that the ID comment of the parental comment be transmitted there). In JS / Ajax I am not strong, I do not know how to do it (

The maximum that I managed - is to pull out the parent ID when you click on the "Comment" button



Although it seems to me that I did it for nothing, because something has already been written in the form on ID ...

Here, then I tried to do so.


<form action='' method='post' role="form" class="form-horizontal form-without-legend default-form">
    {{ comment_form.content }}
    {% if node.parent_id %}
        <input type="hidden" name="parent" id="parent_id" value="{{ node.id }}" />
    {% endif %}
    <button class="btn btn-default">Отправить</button>
</form>


<a href="#" class="answer-btn inline-bloc" data-id={{node.parent_id}}>Комментировать</a>[/icode]


$(document).ready(function(){
        $('.inline-bloc').on('click', function(e){
            e.preventDefault()
            parent_id = $(this).attr('data-id')
            data = {
                parent_id: parent_id
            }
            $.ajax({
                type: "GET",
                url: '',
                data: data,
def comments_view(request):
    comment_form = FeedbackForm(request.POST)
    comments = Feedback.objects.all()
 
    if comment_form.is_valid():
        content = comment_form.cleaned_data['content']
        new_comment = Feedback()
        new_comment.author = request.user
        new_comment.parrent = request.GET.get("parent_id")
        new_comment.content = content
 
    comments.append(new_comment)
 
    return render(request, 'feedback/comment.html', {'comment_form':comment_form, 'comments': comments})
An error occurred, such as the tree does not have an append method.
And here I am at an impasse.

In general, 2 questions:
1. You need to do so that when you click on the "Comment" button "Add a review", because there is hidden form. And passed on the parent's ID.
2. It is necessary to add comments to the tree. Maybe what method is there to add an element to the tree, or maybe I wrote the wrong logic? I did not find the add method in the mptt documentation ... (

[Image: 1014376d1550844850]
Reply
#2
pls, help me :(
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Instagram Bot _ Posting Comments kristianpython 3 3,277 May-23-2020, 12:54 PM
Last Post: kristianpython
  Post comments to Wordpress Blog SergeyLV 1 2,426 Aug-01-2019, 01:38 AM
Last Post: Larz60+
  Cant set api response as tree in Element tree hey_arnold 4 3,635 Mar-04-2019, 03:25 PM
Last Post: dsarin
  BS4 Not Able To Find Text In CSS Comments digitalmatic7 4 5,178 Feb-27-2018, 03:45 AM
Last Post: digitalmatic7
  Need comments on content/style of my first project league55 2 2,962 Jan-24-2018, 08:20 AM
Last Post: league55
  Detect comments part using web scraping seco 7 4,815 Jan-18-2018, 10:06 PM
Last Post: seco

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020