def post(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): form.save() return redirect('blog-home') else: form = PostForm()
def create_post(request): if request.method == 'POST': post = PostForm(request.POST) if post.is_valid(): post.save() return HttpResponseRedirect('/blog') else: post = PostForm() return render_to_response('blog/create_post.html',{'post':post})
def update_post(request, slug): # Check if a user is logged in. if not request.user.is_authenticated(): # No user is logged in. Must be logged in to create/update a post. return HttpResponseRedirect('/login/?next=%s' % request.path) else: # User is logged in. # Check whether a slug has been provided to the view. if slug == '': # No slug has been provided. Instantiate a blank form object. form = PostForm() else: # A slug exists. Display relevant post object in a form for editing. post = get_object_or_404(Post, slug=slug) form = PostForm(instance=post) # Check whether the form has been submitted. if request.method == 'POST': # Check whether or not form submission happened with a pre-existing slug. if slug !='': # Pre-existing post slug exists. form = PostForm(request.POST, instance=post) elif slug =='': # No pre-existing slug. form = PostForm(request.POST) if form.is_valid(): link = form.save(commit=False) # Explicitly associate current user with post object form update. link.user = request.user link.save() return HttpResponseRedirect('../') csrfContext = RequestContext(request,{ "form": form}) return render_to_response('update_post.html', csrfContext)
def update(request,pk): mblogs=Blog.objects.all().order_by('-visitors')[:5] post=get_object_or_404(Blog,pk=pk) if request.method=="POST": form=PostForm(request.POST,instance=post) if form.is_valid(): post=form.save(commit=False) post.user=request.user post.save() return render(request,'blog/detail.html',{'blog':post,'mblogs':mblogs}) else: form=PostForm(instance=post) return render(request,'blog/update.html',{'form':form,'mblogs':mblogs})
def edit(request): mblogs=Blog.objects.all().order_by('-visitors')[:5] if request.method=='POST': form=PostForm(request.POST) if form.is_valid(): post=form.save(commit=False) post.user=request.user post.created_time=datetime.datetime.now() post.published_time=datetime.datetime.now() post.save() return render(request,'blog/detail.html',{'blog':post,'mblogs':mblogs}) else: form=PostForm() return render(request,'blog/edit.html',{'form':form,'mblogs':mblogs})
def new(request): if not request.user.is_staff: messages.error(request, _('You dont have the permissions to create new posts.')) return redirect(index) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.user = request.user post.save() post.subscribers.add(request.user) messages.success(request, 'Your post "%s" was published' % (post.title)) return redirect(post) messages.error(request, _('Some errors were found in your post.')) else: form = PostForm() return render_to_response('blog/post_new.html', {'form': form, 'blog':True, 'django_comments':DJANGO_COMMENTS}, context_instance=RequestContext(request))
def edit(request, post_slug): post = get_object_or_404(Post, slug=post_slug) redirect_url = request.GET.get('next', post) if not request.user.has_perm('blog.change_post'): messages.error(request, _('You dont have the permissions to edit this post.')) return redirect(post) if request.method == 'POST': if request.POST['submit'] == "Save": redirect_url = request.META['PATH_INFO'] form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save() post.subscribers.add(request.user) messages.success(request, 'Your post "%s" was updated.' % (post.title)) return redirect(redirect_url) messages.error(request, _('Some errors were found in your post.')) else: form = PostForm(instance=post) return render_to_response('blog/post_edit.html', {'form': form, 'post':post, 'blog':True, 'django_comments':DJANGO_COMMENTS}, context_instance=RequestContext(request))
def new_post(request): res_dict = {'title': 'New Post', 'form': PostForm(), 'submit': '/blog/new/'} if(request.method == 'POST'): form = PostForm(request.POST) res_dict['form'] = form if(form.is_valid()): new = form.save(commit=False) new.slug = urllib.quote_plus(new.title) new.pub_date = datetime.datetime.now() try: new.save() except IntegrityError as e: res_dict['error'] = 'Sorry, blog title already exists and must be unique' return render(request, 'new_post.html', res_dict) return HttpResponseRedirect('/blog/view/' + new.slug) else: res_dict['error'] = 'Sorry, fields cannot be blank' return render(request, 'new_post.html', res_dict)
def new_post(request): res_dict = { 'title': 'New Post', 'form': PostForm(), 'submit': '/blog/new/' } if (request.method == 'POST'): form = PostForm(request.POST) res_dict['form'] = form if (form.is_valid()): new = form.save(commit=False) new.slug = urllib.quote_plus(new.title) new.pub_date = datetime.datetime.now() try: new.save() except IntegrityError as e: res_dict[ 'error'] = 'Sorry, blog title already exists and must be unique' return render(request, 'new_post.html', res_dict) return HttpResponseRedirect('/blog/view/' + new.slug) else: res_dict['error'] = 'Sorry, fields cannot be blank' return render(request, 'new_post.html', res_dict)