def playlist_edit(request, playlist_id): """ Very similar to playlist_create, but for a Playlist that already exists. """ playlist = Playlist.objects.get(id=playlist_id) items = playlist.playlistitem_set.all() if request.method == 'POST': f = PlaylistForm(request.POST, instance=playlist) items.delete() piforms = [PlaylistItemForm(request.POST, prefix=str(x), instance=PlaylistItem()) for x in range(0,int(request.POST['n-forms']))] if f.is_valid() and all([piform.is_valid() for piform in piforms]): f.save() for piform in piforms: newpi = piform.save(commit=False) newpi.playlist = playlist newpi.save() return redirect('orwell-playlist-index') else: f = PlaylistForm(instance=playlist) piforms = [PlaylistItemForm(prefix=str(x), instance=items[x]) for x in range(0,len(items))] return render_to_response('orwell/edit-playlist.html', {'form':f, 'itemforms':piforms, 'nforms':len(items), 'mode':'Edit', 'butval':'Save'},context_instance=RequestContext(request))
def playlist_create(request): """ If the request is a GET, renders the Playlist editing page for a new Playlist. If it's a POST, creates a new Playlist with the POST data. """ if request.method == 'POST': f = PlaylistForm(request.POST, instance=Playlist()) piforms = [PlaylistItemForm(request.POST, prefix=str(x), instance=PlaylistItem()) for x in range(0,int(request.POST['n-forms']))] if f.is_valid() and all([piform.is_valid() for piform in piforms]): newplaylist = f.save() for piform in piforms: newpi = piform.save(commit=False) newpi.playlist = newplaylist newpi.save() return redirect('orwell-playlist-index') else: f = PlaylistForm(instance=Playlist()) piforms = [PlaylistItemForm(prefix=str(x), instance=PlaylistItem(position=(x+1))) for x in range(0,2)] # two PlaylistItems to start return render_to_response('orwell/edit-playlist.html', {'form':f, 'itemforms':piforms, 'nforms':2, 'mode':'Create', 'butval':'Create'},context_instance=RequestContext(request))