def post(self, request):
     post = Post()
     post.user = request.user  #select user autenticated
     form = PostForm(request.POST, request.FILES, instance=post)
     if form.is_valid():
         form.save()
         return redirect("home_page")
     return render(request, "create_post_form.html", {"form": form})
 def create(self, request):
     post = Post()
     post.user = request.user
     serializer = PostCreateSerializer(data=request.data, instance=post)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
 def post(self, request):
     blog_post = Post()
     blog_post.user = request.user
     form = PostForm(request.POST, instance=blog_post)
     if form.is_valid():
         post = form.save()
         form = PostForm()
         url = reverse("post_detail_page", args=[request.user, post.pk])
         message = "Post created successfully! "
         message += '<a href="{0}">View</a>'.format(url)
         messages.success(request, message)
     return render(request, "post_form.html", {'form': form})
Ejemplo n.º 4
0
 def post(self, request):
     post = Post()
     post.user = request.user
     # post.id = pk
     form = PostForm(request.POST, instance=post)
     if form.is_valid():
         post = form.save()
         form = PostForm()
         url = reverse("post_detail_page", args=[post.user.username, post.pk])
         message = "¡¡ Se ha creado una nueva entrada !!"
         message += '<a href="{0}">Ver</a>'.format(url)
         messages.success(request, message)
     return render(request, "post_form.html", {'form': form})
Ejemplo n.º 5
0
 def post(self, request):
     post = Post()
     post.user = request.user
     post.blog = request.user.blog
     form = PostForm(request.POST, instance=post)
     if form.is_valid():
         post = form.save()
         #vaciamos el formulario
         form = PostForm()
         url = reverse("post_detail", args=[post.user,post.pk]) #reverse genera url pasandole el tipo de URL
         message = " created successfully!"
         message += '<a href="{0}">Create your first post</a>'.format(url)
         #enviamos mensaje de exito con un enlace a la pelicula que acabamos de cr
         messages.success(request, message)
     return render(request, "post_form.html", {'form':form})