def post(self, request, post_id, format=None): form = CommentForm(request.POST) if form.is_valid(): comment = CommentModel() comment.content = request.POST.get('content') comment.post = PostModel.objects.get(id=post_id) comment.author = request.user comment.save() serializer = CommentSerializer(comment) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)
def add_comment(request): check = _check_post_request(request, ['comment', 'post_id']) if check[0]: new_comment = Comment() new_comment.poster = request.user new_comment.text = request.POST['comment'] try: post = Post.objects.get(pk=request.POST['post_id']) new_comment.post = post except Post.DoesNotExist: return HttpResponseBadRequest("There is no Post with id {}".format(request.POST['post_id'])) new_comment.save() return HttpResponseRedirect(reverse('social:home')) else: return HttpResponseBadRequest(check[1])
def add_comment(request): check = _check_post_request(request, ['comment', 'post_id']) if check[0]: new_comment = Comment() new_comment.poster = request.user new_comment.text = request.POST['comment'] try: post = Post.objects.get(pk=request.POST['post_id']) new_comment.post = post except Post.DoesNotExist: return HttpResponseBadRequest("There is no Post with id {}".format( request.POST['post_id'])) new_comment.save() return HttpResponseRedirect(reverse('social:home')) else: return HttpResponseBadRequest(check[1])
data = json.load(dados) for i in data['users']: profile = Profile() profile.name = i['name'] profile.username = i['username'] profile.email = i['email'] profile.street = i['address']['street'] profile.suite = i['address']['suite'] profile.city = i['address']['city'] profile.zipcode = i['address']['zipcode'] profile.save() for j in data['posts']: post = Post() user = Profile.objects.get(id=j['userId']) post.user = user post.title = j['title'] post.body = j['body'] post.save() for k in data['comments']: commet = Comment() post = Post.objects.get(id=k['postId']) commet.post = post commet.name = k['name'] commet.email = k['email'] commet.body = k['body'] commet.save() dados.close()