Home>

There is a code (tree comments), which I wrote not me. It is necessary that the "Delete" button appears next to each commentary, which will delete this comment (but only if the comment does not have attachments)

I practically do not understand the code, but the project needs to do. Please email the finished code and where to insert it, or explain what you need to do

Tree Comments

Base.html _________

{% load comments_tree%}
{% load crispy_forms_tags%}
≪
! DOCTYPE HTML >
≪
HTML LANG= "EN" >
≪
HEAD >
≪
Meta Charset= "UTF-8" >
≪
link rel= "stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap .." integrity= "sha384-Gn5384xqQ1aoWXA + 058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW /dAiS6JXm" crossorigin= "anonymous" >
≪
Title >
Title <
/title >
≪
/Head >
≪
Body >
≪
DIV Class= "Row" >
≪
DIV CLASS= "COL-MD-3" >
≪
/div >
≪
DIV CLASS= "COL-MD-6" >
{{Comments | Comments_Filter}}
≪
HR >
≪
DIV CLASS= "COL-MD-12" >
≪
Form Action= "{% URL 'COMMENT_CREATE'%}" Method= "POST" >
{% csrf_token%}
{{COMMENT_FORM | crispy}}
≪
INPUT TYPE= "Submit" class= "BTN BTN-PRIMARY" VALUE= "SEND" >
≪
/Form >
≪
/div >
≪
/div >
≪
DIV CLASS= "COL-MD-3" >
≪
/div >
≪
/div >
≪
/Body >
≪
script.
src= "https://code.jquery.com/jquery-3.6.0.js"
integrity= "SHA256-H + K7U5CNXL1H5YWQFKTSJ8PCMON9AAQ30GDH27XC0JK="
Crossorigin= "Anonymous" >
≪
/script >
≪
script src= "https: //maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap ..." integrity= "sha384-JZR6Spejh4U02d8jOt6vLEHfe /JQGiRRSQQxSfFWpi1MquVdAyjUar5 + 76PVCmYl" crossorigin= "anonymous" >
≪
/script >
≪
script >
$ (Document) .ready (Function () {
$ (". Reply".) ON ('Click', Function () {
VAR ParentID= $ (this) .attr ('Data-ID')
$ ("# Form -" + Parentid) .fadetoggle ();
})
$ (". submit-reply"). ON ('Click', Function (E) {
E.PreventDefault ()
VAR Parentid= $ (this) .attr ('Data-submit-reply')
VAR ID= $ (this) .attr ('Data-ID')
Var Text= $ ("# Form -'+ id) .find (' Textarea [Name="Comment-Text "] '). Val ();
FUNCTION GETCOOKIE (NAME) {
Let CookieValue= NULL;
If (Document.Cookie &
&
 Document.Cookie!== '') {
const cookies= document.cookie.split (';');
For (Let i= 0; I <
 cookies.Length; I ++) {
Const Cookie= Cookies [i] .trim ();
//Does This Cookie String Begin Went?
If (cookie.substring (0, name.length + 1)=== (Name + '=')) {
Cookievalue= Decodeuricomponent (cookie.substring (name.length + 1));
Break;
}
}
}
Return CookieValue;
}
const CSRFToken= GetCookie ('CSRFToken');
Data= {
User: "{{Request.user.userName}}"
Parentid: Parentid,
TEXT: Text,
ID: Id,
CSRFMIDDleWeartoken: Csrftoken.
}
$.ajax ({
Method: "POST",
Data: Data,
URL: "{% URL 'COMMENT_CHILD_CREATE'%}"
SUCCESS: FUNCTION (DATA) {
Window.Location.Replace ('Post-Comments')
}
})
})
})
≪
/script >
≪
/HTML >

comments_tree.py ___________

from django.template import library
From django.utils.html Import Mark_Safe
REGISTER= LIBRARY ()
@ register.filter.
Def Comments_Filter (Comments_List):
res= "" "
≪
UL >
 STYLE= "LIST-STYLE-TYPE: NONE;" >
≪
DIV CLASS= "COL-MD-12 MT-2" >
{}
≪
/div >
≪
/ul >
"" "
i= ''
For comment in comments_list:
I += "" "
≪
Li >
≪
div class= "COL-MD-12 MB-2 MT-2 P-0" >
≪
Small >
{Author} <
/Small >
 | Published: {TimeStamp}
≪
HR >
≪
P >
{Text} | id= {id} <
/P >
≪a href= "#" class= "reply" data-id= "{ID}" Data-Parent= {Parent_ID} > Reply <
/a >
≪
Form Action "" Method= "POST" Class= "Comment-Form Form-Group" ID "Form-{ID}" Style= "Display: None;" >
≪
Textarea Type= "Text" class= "Form-Control" name= "comment-text" >
≪
/TEXTAREA >
≪
BR >
≪
Input Type= "Submit" class= "BTN BTN-PRIMARY submit-reply" data-id= "{ID}" Data-submit-reply= "{parent_id}" value= "Send" >
≪
/Form >
≪
/div >
≪
/Li >
"" ".Format (id= Comment ['id'],
 Author= Comment ['Author'],
 TimeStamp= Comment ['TimeStamp'],
 Text= Comment ['Text'],
 parent_id= comment ['parent_id'])
If comment.get ('Children'):
I += Comments_Filter (COMMENT ['Children'])
Return Mark_Safe (Res.Format (I))

models.py __________

from django.db import models
From Django.conf Import Settings
from django.contrib.contenttypes.models import contenttype
from django.contrib.contenttypes.fields Import Genericrelation
Class Post (Models.Model):
Author= Models.ForeignKey (settings.auth_user_model, verbose_name= 'author', on_delete= models.cascade)
title= models.charfield (max_length= 255, verbose_name= 'Name post')
Text= Models.TextField ()
Comments= GenericRelation ('Comment')
DEF __STR __ (SELF):
Return Self.title
Class COMMENT (Models.Model):
User= Models.ForeignKey (settings.auth_user_model, verbose_name= 'author', on_delete= Models.cascade)
Text= Models.TextField (verbose_name= 'Comment Text')
Parent= Models.ForeignKey (
'self',
verbose_name= 'Parent Comment',
Blank= True,
NULL= TRUE
related_name= 'comment_children',
ON_DELETE= Models.cascade.
)
Content_Type= Models.ForeignKey (ContentType, On_Delete= Models.cascade)
Object_ID= Models.positiveIntegerfield ()
TimeStamp= Models.dateTimeField (Auto_Now= True, Verbose_Name= 'Commetaria creation date')
is_child= models.booleanfield (default= false)
DEF __STR __ (SELF):
RETURN STR (Self.ID)
@Property.
DEF Get_Parent (Self):
If not self.parent:
Return ""
Return Self.Parent.

views.py __________

from django.shortcuts import render
from django.db import transaction
from django.http import httpresponseredirect
from django.contrib.auth.Models Import User
from django.contrib.contenttypes.models import contenttype
from .models Import Post, Comment
from .forms import commentForm
from .utils import create_comments_tree
Def Base_View (Request):
Comments= post.Objects.first (). comments.all ()
Result= Create_Comments_Tree (Comments)
Comment_form= CommentForm (Request.post or None)
Return Render (Request, 'Base.html', {'Comments': Result, 'Comment_Form': Comment_form})
DEF CREATE_COMMENT (Request):
Comment_form= CommentForm (Request.post or None)
if comment_form.is_valid ():
new_comment= comment_form.save (COMMIT= FALSE)
new_comment.user= request.user.
new_comment.text= comment_form.cleaned_data ['Text']
new_comment.content_type= contenttype.objects.get (Model= 'Post')
new_comment.objects_id= 1.
new_comment.parent= none
new_comment.is_child= false
new_comment.save ()
RETURN HTTPRESPONSEREDIRECT ('/POST-COMMENTS')
@ transaction.atomic
DEF CREATE_CHILD_COMMENT (Request):
User_Name= Request.post.get ('User')
current_id= request.post.get ('id')
Text= Request.post.get ('Text')
user= user.Objects.get (username= user_name)
Content_Type= contentType.Objects.get (Models= 'Post')
Parent= comment.Objects.get (id= int (current_id))
is_child= false if not parent else true
Comment.objects.create (
User= User, Text= Text, Content_Type= content_type, object_id= 1,
Parent= Parent, Is_Child= Is_Child
)
comments_= post.Objects.first (). comments.all ()
Comments_List= Create_Comments_Tree (Comments_)
Return Render (Request, 'Base.html', {'Comments': Comments_List})

This is not Freelance Exchange

Darth KYL2021-04-07 05:54:30

So I do not give the project

Евгений2021-04-07 05:58:31

I have a specific problem that I do not know how to solve

Евгений2021-04-07 05:59:48

Try to google?

Andrew Holovko2021-04-07 06:12:20

If we answer your question, you will further ask? Read Django documentation, template syntax, HTML basics.

Andrew Holovko2021-04-07 07:47:16