def new(request): if request.method == 'POST': print(request.user) form = AddNewWorkoutForm(request.POST) if form.is_valid(): if request.user.is_anonymous(): context = {'anonerror': True, 'form': form} return render(request, 'workout/new.html', context) else: workout = Workout(category=form.cleaned_data['category'], detail=form.cleaned_data['detail']) workout.save() context = {'workout': workout} return render(request, 'workout/added.html', context) else: form = AddNewWorkoutForm() context = {'form': form} return render(request, 'workout/new.html', context)
def add(request): """ Add View ------------ Fill out a CreateForm for a workout Simple form entry here, maybe a default view? Need dropdown for workout options """ context_data = {} if request.method == 'POST': ''' Form has been submitted. Run checks and cleans and save ''' # Get all data here workout = Workout() workout.duration = request.POST['duration'] if 'with_other_class' in request.POST: if request.POST['with_other_class'] == 'on': workout.with_other_class = True # Manage Dates try: workout.workout_date = request.POST['workout_date'] except: year = request.POST['workout_date_year'] month = request.POST['workout_date_month'] day = request.POST['workout_date_day'] workout.workout_date = '{}-{}-{}'.format(year, month, day) workout.activity_id = request.POST['activity'] # Populate user data user = request.user workout.user_id = user.pk # Create bound form # form = WorkoutFormCreate(data = workout_data ) try: # VALID DATA workout.save() context_data['message'] = "Data submitted." return render_to_response( 'workout/workouts_submit.html', context_data, context_instance=RequestContext(request), ) except: # INVALID DATA context_data['message'] = \ "Invalid Data Submitted. Please try again." ''' ELSE if request.method != POST) Form has not yet been submitted. Create new form. ''' # Create unbound form context_data['form'] = WorkoutFormCreate(user=request.user) # form = WorkoutFormCreate(user = request.user ) return render_to_response( 'workout/workout_form.html', context_data, context_instance=RequestContext(request), )