def add_unit(request, id): # Get the context from the request. context = RequestContext(request) project = get_object_or_404(Project, id=id) # A HTTP POST? if request.method == 'POST': initial = {'project': project} form = UnitForm(request.POST or None, initial=initial) # Have we been provided with a valid form? if form.is_valid(): # Save the new projecto to the database. unit = form.save(commit=False) unit.project = project unit.save() return redirect('project', id) else: print form.errors else: # If the request was not a POST, display the form to enter details. form = UnitForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). # return render_to_response('unit/add_project.html', {'form': form}, context) #return render_to_response('unit/add_project.html', {'form': form}, context) return render(request, 'unit/add_unit.html', {'form': form})
def edit_unit(request, id): unit = get_object_or_404(Unit, id=id) form = UnitForm(request.POST or None, instance=unit) success = False if form.is_valid(): form.save() success = True #return redirect('add_unit', id) #redirect_url = reverse(edit_unit(request,id)) #return HttpResponseRedirect(redirect_url) return render(request, 'unit/add_unit.html', {'form': form, 'success': success,})
def newUnit(request): if request.method == "GET": form = UnitForm() roles = Position.objects.filter(youth=False).exclude(id=7) # Guardian #send_mail('Subject', 'Here is the message.', '*****@*****.**', #['*****@*****.**'], fail_silently=False) return 'unit/new_unit.html', {'form':form, 'roles':roles} else: unitform = UnitForm(request.POST) if unitform.is_valid(): unit = unitform.save() request.user.profile.unit = unit pos = Position.objects.get(pk=unitform.cleaned_data['user_role']) request.user.profile.position = pos; request.user.profile.save() return HttpJsonSuccess() else: return HttpJsonFormError(unitform.errors)