def new_movie(request): if request.method == "POST": form = MovieForm(request.POST) if form.is_valid(): if form.save(): return redirect("/movies") else: form = MovieForm() data = {'form': form} return render(request, "movies/new_movie.html", data)
def edit_movie(request, movie_id): movie = Movie.objects.get(id=movie_id) if request.method == "POST": form = MovieForm(request.POST, instance=movie) if form.is_valid(): if form.save(): return redirect("/movies/{}".format(movie_id)) else: form = MovieForm(instance=movie) data = {"movie": movie, "form": form} return render(request, "movies/edit_movie.html", data)
def new_movie(request): # If the user is submitting the form if request.method == "POST": # Get the instance of the form filled with the submitted data form = MovieForm(request.POST) # Django will check the form's validity for you if form.is_valid(): # Saving the form will create a new Genre object if form.save(): # After saving, redirect the user back to the index page return redirect("/movies") # Else if the user is looking at the form page else: form = MovieForm() data = {'form': form} return render(request, "new_movie.html", data)
def edit_movie(request, movie_id): # Similar to the the detail view, we have to find the existing genre we are editing movie = Movie.objects.get(id=movie_id) # We still check to see if we are submitting the form if request.method == "POST": # We prefill the form by passing 'instance', which is the specific # object we are editing form = MovieForm(request.POST, instance=movie) if form.is_valid(): if form.save(): return redirect("/movies/{}".format(movie_id)) # Or just viewing the form else: # We prefill the form by passing 'instance', which is the specific # object we are editing form = MovieForm(instance=movie) data = {"movie": movie, "form": form} return render(request, "edit_movie.html", data)