def addPost(request,form=None): if not request.method == "POST": form = PostsForm() context = { "form":form } return render(request,"add.html", context) else: form_to_save = PostsForm(request.POST) if form_to_save.is_valid(): form_to_save.save() return redirect('/blog/')
def editPost(request, id): post = get_object_or_404(Posts,pk=id) if request.method == "POST": form = PostsForm(request.POST, instance=post) if form.is_valid(): post.save() return redirect('/blog/') else: form = PostsForm(instance=post) context = { "form":form } return render(request, "edit.html", context)
def get_posts(request): all_posts = Post.objects.all() if request.POST: print("HEREEEEEEEEEEEEEEE") form = PostsForm(request.POST) categories = list() res_posts = list() if form.is_valid(): for cat in form.cleaned_data.get("categories"): category = PostsCategories.objects.filter(title=cat) if category.count(): categories.append(category.first()) for post in all_posts: added = False for cat in form.cleaned_data.get("categories"): if post.categories.filter(title=cat): added = True break if added: res_posts.append(post) return render(request, "posts/posts.html", dict(all_posts=res_posts, form=form)) form = PostsForm() return render(request, "posts/posts.html", dict(all_posts=all_posts, form=form))