def tips_edit(request, slug): tip = get_object_or_404(Tip, slug=slug) if request.method == 'POST': form = TipForm(instance=tip, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect( '/' ) else: form = TipForm(instance = tip) return render_to_response('tips_add.html', { 'form': form, 'add' : False, })
def tips_add(request): if request.method == 'POST': form = TipForm(request.POST) # ModelChoiceField # form.save() went into the parent object (Tip), and pulled in # the child objects from their respective models (e.g., City, Category, etc) # So this way, form.category = 1 ---> form.category_id = 1, form.category = <Category object> #assert False if form.is_valid(): city_ac = request.POST.get('city_ac','') newtip = form.save(commit=False) newtip.user = request.user newtip.ups = 0 newtip.downs = 0 newtip.date = datetime.date.today() # City was not autocompleted, create a new record for it, and add it to the tip if city_ac == 'False': newcity_name = request.POST.get('city_name', '') newcity = City(name=newcity_name, country=newtip.country) newcity.save() newtip.city = newcity newtip.save() return HttpResponseRedirect( '/' ) else: form = TipForm() return render_to_response('tips_add.html', { 'form': form, 'add': True, 'user':request.user, })