def post(self, request): post_with_owner = Post() self.check_permissions(request) # compruebo si el usuario autenticado puede hacer POST blog = Blog.objects.filter(owner=request.user).all() post_with_owner.blog = blog[0] post_with_owner.owner = request.user # asigno como propietario de la foto, el usuario autenticado post_with_owner.visibility = POSTED serializer = PostCreateSerializer(instance=post_with_owner, data=request.data) if serializer.is_valid(): serializer.save() # Guarda el objeto Photo y me lo devuelve return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): """ Crea un post en base a la información POST :param request: HttpRequest :return: HttpResponse """ success_message = "" post_with_owner = Post() blog = self.get_blogs_queryset(request.user.username).all() post_with_owner.blog = blog[0] post_with_owner.owner = request.user # asigno como propietario de la foto, el usuario autenticado form = PostForm(request.POST, instance=post_with_owner) if form.is_valid(): new_post = form.save() # Guarda el objeto Photo y me lo devuelve form = PostForm() success_message = "Guardado con éxito!" success_message += '<a href="{0}">'.format( reverse("post_detail", args=[new_post.blog.owner.username, new_post.pk]) ) success_message += "Ver post" success_message += "</a>" context = {"form": form, "success_message": success_message} return render(request, "post/new_post.html", context)