def post_new(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = request.user post.published_date = timezone.now() post.save() return redirect('post_detail', pk=post.pk) else: form = PostForm() return render(request, 'blog/post_edit.html', {'form': form})
def edit_post(request, pk): post = Post.objects.get(id=int(pk)) if request.user.username != post.user.username: raise PermissionDenied #permission denied if request.method == "GET": template_name = 'blog/edit_post.html' context = {'post': post} return render(request, template_name,context) else: post.title = request.POST['title'] post.content = request.POST['content'] if 'img' in request.FILES: post.img = request.FILES["img"] if 'is published' in request.POST: post.is_published = request.POST['is_published'] post.save() return redirect('post', post.id)