def create(request): if request.method == 'POST': category = Category() form = CategoryForm(request.POST, instance=category) if form.is_valid(): form.save() return redirect('/categorias') else: return render_to_response('category_new.html', locals(), context_instance=RequestContext(request))
def update(request, category_id): if request.method == 'POST': category = Category.objects.get(id=category_id) form = CategoryForm(request.POST, instance=category) if form.is_valid(): form.save() return redirect('/categorias') else: return render_to_response('category_edit.html', locals(), context_instance=RequestContext(request))
def addCategory(request): config = ConfigParser.ConfigParser() config.read('moo.ini') host = config.get("bottle","host") port = config.get("bottle","port") template = 'welcome.html' params = {'':''} if request.method == 'POST': form = CategoryForm(request.POST) title = request.POST['title'] if form.is_valid(): title = request.POST['title'] print 'Category added.' url='http://'+host+':'+port+'/category' name = request.POST['name'] description = request.POST['description'] now = datetime.datetime.now() createDate = now.month,'-',now.day,'-',now.year status = '0' method = 'POST' args = {'name':name, 'description':description, 'createDate':createDate,'status':status} encoded_args = urllib.urlencode(args) request = urllib2.Request(url,encoded_args) request.get_method = lambda: method try: response = urllib2.urlopen(request) template = 'welcome.html' r=response.read() result=json.loads(r) cid = result['category']['id'] params = {'result':'Category is added succesfully! Category id:'+cid} except urllib2.HTTPError, e: if(e.code == 409): template = 'category/category.html' params = {'result':'Category Name is duplicated'} else: template = 'category/category.html' params = {'result':'Server error, try later'} else: print 'Invalid form entered. Populate data on form.' name = request.POST['name'] description = request.POST['description'] print name, description params = {'name':name, 'description':description,'result':'Required fields are not entered'} template = 'category/category.html'
def add_category(request): # immediately get the context - as it may contain posting data context = RequestContext(request) if request.method == 'POST': # data has been entered into the form via Post form = CategoryForm(request.POST) if form.is_valid(): # the form has been correctly filled in, # so lets save the data to the model cat = form.save(commit=True) # show the index page with the list of categories return index(request) else: # the form contains errors, # show the form again, with error messages pass else: # a GET request was made, so we simply show a blank/empty form. form = CategoryForm() # pass on the context, and the form data. return render_to_response('rango/add_category.html', {'form': form}, context)