def create_comment_view(request, post_id): if request.method == 'POST': post = Post.objects.get(pk=post_id) if request.user.is_authenticated(): if post: comment = Comment() comment.body = request.POST['body'] comment.pub_date = timezone.datetime.now() comment.post = post comment.author = request.user comment.save() return JsonResponse( { "status": "success", "message": "answer created", "comment_id": comment.pk }, status=201) else: return JsonResponse( { "status": "failure", "message": "post not exist" }, status=404) else: return JsonResponse( { "status": "failure", "message": "user need to login" }, status=403) else: return render(request, 'posts/create-commentd.html', {'post_id': post_id})
def create_comment(request, id): post = get_object_or_404(Post, id=id) if request.method == "POST": comment = Comment() comment.body = request.POST['body'] comment.post = post comment.save() return redirect('posts:retrieve', id=post.id)
def import_archive(): with open("comments.json", "r") as read_file: data = json.load(read_file) with open("users.json", "r") as read_file: data1 = json.load(read_file) with open("posts.json", "r") as read_file: data2 = json.load(read_file) with open("address.json", "r") as read_file: data3 = json.load(read_file) for i in range(len(data['comments'])): comments = Comment() comments.body = data['comments'][i]["body"] comments.email = data['comments'][i]["email"] comments.postId = data['comments'][i]["postId"] comments.id = data['comments'][i]["id"] comments.name = data['comments'][i]["name"] comments.save() for i in range(len(data2['posts'])): posts = Post() posts.body = data2['posts'][i]["body"] posts.userId = data2['posts'][i]["userId"] posts.title = data2['posts'][i]["title"] posts.id = data2['posts'][i]["id"] posts.save() for i in range(len(data1['users'])): users = Profile() users.username = data1['users'][i]["username"] users.address = data1['address'][i]["id"] users.email = data1['users'][i]["email"] users.save() for post in data2['posts']: profile = Profile.objects.get(id=post['userId']) Comment.objects.create(id=comment['id'], name=comment['name'], email=comment['email'], body=comment['body'], post=post)