def edit_post(request, **kwargs): logger.debug("edit_post:") # PostForm = forms.form_for_model(Post) p = Post.objects.get(slug=kwargs['slug']) if request.POST: logger.info("Got POST...") form = PostForm(request.POST, instance=p) if form.is_valid(): logger.info("Form is valid, saving...") # logger.debug(form) model_instance = form.save(commit=False) model_instance.update_date = timezone.now() model_instance.save() # messages.add_message(request, messages.INFO, "Saved '%s'" % model_instance.title) messages.info(request, "Saved '%s'" % model_instance.title) # return HttpResponseRedirect(model_instance.get_absolute_url()) # c = RequestContext(request) # c['form']=form # return HttpResponseRedirect(t.render(c)) # next line is required to save the many-to-many relations form.save_m2m() else: logger.warn("Form data invalid; showing again") logger.warn("Form errors: %s" % form.errors) messages.error(request, form.errors) # logger.debug(form) c = RequestContext(request, {'form': form}) t = loader.get_template('xblog/edit_post.html') # messages.add_message(request, messages.ERROR, form.errors) return HttpResponse(t.render(c)) else: # f = forms.form_for_instance(p,form=PostForm)() form = PostForm(instance=p) c = RequestContext(request, {'form': form}) t = loader.get_template('xblog/edit_post.html') return HttpResponse(t.render(c))
def add_post(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): logging.debug(request.POST) # commit=False means the form doesn't save at this time. # commit defaults to True which means it normally saves. model_instance = form.save(commit=False) model_instance.update_date = timezone.now() model_instance.save() # messages.add_message(request, messages.INFO, "Added '%s'" % model_instance.title) logging.debug(form) messages.info(request, "Added '%s'" % model_instance.title) form.save_m2m() return redirect(model_instance.get_absolute_url()) else: # messages.add_message(request, messages.ERROR, form.errors) messages.error(request, form.errors) c = RequestContext(request) c['form'] = form t = loader.get_template('xblog/edit_post.html') return HttpResponse(t.render(c)) else: author = request.user # set the default values default_blog = Blog.objects.get(owner=request.user) logging.debug("Blog %s" % default_blog) author = request.user logging.debug("Author: %s" % author) default_status = "draft" default_text_filter = "markdown" form = PostForm( initial={ "author": request.user, "blog": default_blog, "status": default_status, "text_filter": default_text_filter }) return render(request, "xblog/edit_post.html", {'form': form})
def edit_post(request, **kwargs): logger.debug("edit_post:") # PostForm = forms.form_for_model(Post) p = Post.objects.get(slug=kwargs['slug']) if request.POST: logger.info("Got POST...") form = PostForm(request.POST, instance=p) if form.is_valid(): logger.info("Form is valid, saving...") # logger.debug(form) model_instance = form.save(commit=False) model_instance.update_date = timezone.now() model_instance.save() # messages.add_message(request, messages.INFO, "Saved '%s'" % model_instance.title) messages.info(request, "Saved '%s'" % model_instance.title) # return HttpResponseRedirect(model_instance.get_absolute_url()) # c = RequestContext(request) # c['form']=form # return HttpResponseRedirect(t.render(c)) # next line is required to save the many-to-many relations form.save_m2m() else: logger.warn("Form data invalid; showing again") logger.warn("Form errors: %s" % form.errors) messages.error(request, form.errors) # logger.debug(form) c = RequestContext(request, {'form': form}) t = loader.get_template('xblog/edit_post.html') # messages.add_message(request, messages.ERROR, form.errors) return HttpResponse(t.render(c)) else: # f = forms.form_for_instance(p,form=PostForm)() form = PostForm(instance=p) c = RequestContext(request, {'form':form}) t = loader.get_template('xblog/edit_post.html') return HttpResponse(t.render(c))
def add_post(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): logging.debug(request.POST) # commit=False means the form doesn't save at this time. # commit defaults to True which means it normally saves. model_instance = form.save(commit=False) model_instance.update_date = timezone.now() model_instance.save() # messages.add_message(request, messages.INFO, "Added '%s'" % model_instance.title) logging.debug(form) messages.info(request, "Added '%s'" % model_instance.title) form.save_m2m() return redirect(model_instance.get_absolute_url()) else: # messages.add_message(request, messages.ERROR, form.errors) messages.error(request, form.errors) c = RequestContext(request) c['form']=form t = loader.get_template('xblog/edit_post.html') return HttpResponse(t.render(c)) else: author = request.user # set the default values default_blog = Blog.objects.get(owner=request.user) logging.debug("Blog %s" % default_blog) author = request.user logging.debug("Author: %s" % author) default_status = "draft" default_text_filter = "markdown" form = PostForm(initial={"author":request.user, "blog" : default_blog, "status" : default_status, "text_filter": default_text_filter}) return render(request, "xblog/edit_post.html", {'form': form})