def new_post(request, thread_id): thread = get_object_or_404(Thread, pk=thread_id) if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(False) post.thread = thread post.user = request.user post.save() messages.success(request, "Your post has been added to the thread!") return redirect(reverse('thread', args={thread.pk})) else: form = PostForm() args = { 'form': form, 'form_action': reverse('new_post', args={thread.id}), 'button_text': 'Update Post' } args.update(csrf(request)) return render(request, 'post_form.html', args)
def edit_post(request, thread_id, post_id): thread = get_object_or_404(Thread, pk=thread_id) post = get_object_or_404(Posts, pk=post_id) if request.method == "POST": form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save() messages.success(request, "You have updated your thread") return redirect(reverse('thread', args={thread.pk})) else: form = PostForm(instance=post) args = { 'form': form, 'form_action': reverse('edit_post', kwargs={"thread_id": thread.id, "post_id": post.id}), 'button_text': 'Update Post' } args.update(csrf(request)) return render(request, 'post_form.html', args)
def new_post(request, thread_id): """ Create new post then return to previous (paginated) page """ thread = get_object_or_404(Thread, pk=thread_id) redirect_to = request.GET.get('next', '') if request.method == "POST": form = PostForm(request.POST, request.FILES) if form.is_valid(): post = form.save(False) post.thread = thread post.user = request.user post.save() messages.success(request, "Your post has been added to the thread!") return redirect(redirect_to) else: form = PostForm() args = {'form': form, 'button_text': 'Save Post'} args.update(csrf(request)) return render(request, 'forum/post_form.html', args)
def edit_post(request, thread_id, post_id): thread = get_object_or_404(Thread, pk=thread_id) post = get_object_or_404(Posts, pk=post_id) if request.method == "POST": form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save() messages.success(request, 'Your have updated your thread!') return redirect(reverse('thread', args={thread.pk})) else: form = PostForm(instance=post) args = { 'form': form, 'form_action': reverse('edit_post', kwargs={ 'thread_id': thread.id, 'post_id': post.id }), 'button_text': 'Update Post' } args.update(csrf(request)) return render(request, 'post_form.html', args)
def new_thread(request, subject_id): subject = get_object_or_404(Subject, pk=subject_id) poll_subject_formset = formset_factory(PollSubjectForm, extra=3) if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) poll_form = PollForm(request.POST) poll_subject_formset = poll_subject_formset(request.POST) if thread_form.is_valid() and post_form.is_valid() and poll_form.is_valid() and poll_subject_formset.is_valid(): thread = thread_form.save(False) thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread post.save() poll = poll_form.save(False) poll.thread = thread poll.save() for subject_form in poll_subject_formset: subject = subject_form.save(False) subject.poll = poll subject.save() messages.success(request, "You have created a new thread!") return redirect(reverse('thread', args={thread.pk})) else: thread_form = ThreadForm() post_form = PostForm(request.POST) poll_form = PollForm() poll_subject_formset = poll_subject_formset() args = { 'thread_form': thread_form, 'post_form': post_form, 'subject': subject, 'poll_form': poll_form, 'poll_subject_formset': poll_subject_formset, } args.update(csrf(request)) return render(request, 'thread_form.html', args)
def edit_post(request, thread_id, post_id): """ Edit post if created by user, then return to previous (paginated) page """ thread = get_object_or_404(Thread, pk=thread_id) post = get_object_or_404(Post, pk=post_id) redirect_to = request.GET.get('next', '') if request.method == "POST": form = PostForm(request.POST, instance=post) if form.is_valid(): form.save() messages.success(request, "You have updated your post!") return redirect(redirect_to) else: form = PostForm(instance=post) args = {'form': form, 'button_text': 'Update Post'} args.update(csrf(request)) return render(request, 'forum/post_form.html', args)